﻿using Coldairarrow.Business.AOP;
using Coldairarrow.Entity.Base;
using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.FP;
using Coldairarrow.Entity.PB;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using Coldairarrow.Util.Helper;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Coldairarrow.Business.FP
{
    public partial class Base_OverFlowBusiness : BaseBusiness<Base_OverFlow>, IBase_OverFlowBusiness, ITransientDependency
    {
        readonly IOperator _operator;
        public Base_OverFlowBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口
        public async Task<PageResult<Base_OverFlow>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<Base_OverFlow>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<Base_OverFlow, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<List<Base_OverFlowTask>> GetTaskDataListAsync()
        {
            List<Base_OverFlowTask> result = new List<Base_OverFlowTask>();
            //获取工作流配置
            var basecfg = await Db.GetListBySqlAsync<Base_OverFlowCfg>("select a.BillId,a.BillType,a.Level,b.Id,b.Title,b.Content from Base_OverFlowTask a left join Base_OverFlow b on a.BillType=b.BillType");
            if (basecfg.Count > 0)
            {
                //
                foreach (var cfg in basecfg)
                {
                    switch (cfg.BillType)
                    {
                        case "DeliveryNotice":
                            #region 发货通知
                            var PB_DeliveryModel = await Db.GetIQueryable<PB_DeliveryNotice>().Where(m => m.Id == cfg.BillId).FirstOrDefaultAsync();
                            if (PB_DeliveryModel != null)
                            {
                                var PB_DeliveryModelList = await Db.GetIQueryable<PB_DeliveryNoticeItem>().Where(x => x.ParentId == PB_DeliveryModel.Id).ToListAsync();
                                String Bulidno = "";
                                foreach (var item in PB_DeliveryModelList)
                                {
                                    Bulidno += item.BuildingNo;
                                }
                                //有工作流的
                                JArray jarr = JArray.Parse(cfg.Content);
                                JObject jobj = JObject.Parse(jarr[cfg.Level].ToString());
                                string txt = jobj["StartState"]["Name"].ToString();
                                string rid = jobj["Assignees"]["RoleId"].ToString();
                                string btn = jobj["Action"]["Name"].ToString();
                                JArray usr = JArray.Parse(jobj["Assignees"]["UserId"].ToString());
                                if (usr.Count() > 0)
                                {
                                    foreach (var item in usr)
                                    {
                                        string _u = item.ToString();
                                        if (_u.Equals(_operator.UserId))
                                        {
                                            Base_OverFlowTask temp = new Base_OverFlowTask();
                                            temp.Id = cfg.Id;
                                            temp.BillType = cfg.BillType;
                                            var Base_EnumItemModel = await Db.GetIQueryable<Base_EnumItem>().Where(m => m.Code == cfg.BillType && m.EnumCode == "BillType").FirstOrDefaultAsync();
                                            string aa = "";
                                            if (Base_EnumItemModel != null)
                                            {
                                                aa = Base_EnumItemModel.Name;
                                            }
                                            temp.BillId = cfg.BillId;
                                            temp.Title = DateTime.Now.ToString("g") + aa + "(" + txt + ")";
                                            temp.BtnName = btn;
                                            temp.ProjectName = PB_DeliveryModel.ProjectName + Bulidno;
                                            temp.Rule = 0;
                                            result.Add(temp);
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    //角色
                                    if (_operator.Property.RoleIdList.Contains(rid))
                                    {
                                        Base_OverFlowTask temp = new Base_OverFlowTask();
                                        temp.Id = cfg.Id;
                                        temp.BillId = cfg.BillId;
                                        temp.BillType = cfg.BillType;
                                        var Base_EnumItemModel = await Db.GetIQueryable<Base_EnumItem>().Where(m => m.Code == cfg.BillType && m.EnumCode == "BillType").FirstOrDefaultAsync();
                                        string aa = "";
                                        if (Base_EnumItemModel != null)
                                        {
                                            aa = Base_EnumItemModel.Name;
                                        }
                                        temp.Title = DateTime.Now.ToString("g") + aa + "(" + txt + ")";
                                        temp.ProjectName = PB_DeliveryModel.ProjectName + Bulidno;
                                        temp.Rule = 0;
                                        result.Add(temp);
                                    }
                                }

                            }
                            #endregion
                            break;
                        case "MobilizationNotice":
                            #region 进场通知
                            var PB_MobilizationNoticeModel = await Db.GetIQueryable<PB_MobilizationNotice>().Where(m => m.Id == cfg.BillId).FirstOrDefaultAsync();
                            if (PB_MobilizationNoticeModel != null)
                            {
                                var PB_MobilzationModeList = await Db.GetIQueryable<PB_MobilzationItem>().Where(x => x.ParentId == PB_MobilizationNoticeModel.Id).ToListAsync();
                                String Bulidno = "";
                                foreach (var item in PB_MobilzationModeList)
                                {
                                    Bulidno += item.BuildingNo;
                                }
                                //有工作流的
                                JArray jarr = JArray.Parse(cfg.Content);
                                JObject jobj = JObject.Parse(jarr[cfg.Level].ToString());
                                string txt = jobj["StartState"]["Name"].ToString();
                                string rid = jobj["Assignees"]["RoleId"].ToString();
                                string btn = jobj["Action"]["Name"].ToString();
                                JArray usr = JArray.Parse(jobj["Assignees"]["UserId"].ToString());
                                if (usr.Count() > 0)
                                {
                                    foreach (var item in usr)
                                    {
                                        string _u = item.ToString();
                                        if (_u.Equals(_operator.UserId))
                                        {
                                            Base_OverFlowTask temp = new Base_OverFlowTask();
                                            temp.Id = cfg.Id;
                                            temp.BillType = cfg.BillType;
                                            temp.BillId = cfg.BillId;
                                            var Base_EnumItemModel = await Db.GetIQueryable<Base_EnumItem>().Where(m => m.Code == cfg.BillType && m.EnumCode == "BillType").FirstOrDefaultAsync();
                                            string aa = "";
                                            if (Base_EnumItemModel != null)
                                            {
                                                aa = Base_EnumItemModel.Name;
                                            }
                                            temp.Title = DateTime.Now.ToString("g") + aa + "(" + txt + ")";
                                            temp.BtnName = btn;
                                            temp.ProjectName = PB_MobilizationNoticeModel.ProjectName + Bulidno;
                                            temp.Rule = 0;
                                            result.Add(temp);
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    //角色
                                    if (_operator.Property.RoleIdList.Contains(rid))
                                    {
                                        Base_OverFlowTask temp = new Base_OverFlowTask();
                                        temp.Id = cfg.Id;
                                        temp.BillId = cfg.BillId;
                                        temp.BillType = cfg.BillType;
                                        var Base_EnumItemModel = await Db.GetIQueryable<Base_EnumItem>().Where(m => m.Code == cfg.BillType && m.EnumCode == "BillType").FirstOrDefaultAsync();
                                        string aa = "";
                                        if (Base_EnumItemModel != null)
                                        {
                                            aa = Base_EnumItemModel.Name;
                                        }
                                        temp.Title = DateTime.Now.ToString("g") + aa + "(" + cfg.Title + ")";
                                        temp.ProjectName = PB_MobilizationNoticeModel.ProjectName + Bulidno;
                                        temp.Rule = 0;
                                        result.Add(temp);
                                    }
                                }

                            }
                            #endregion
                            break;
                        case "ReturnNotice":
                            #region 回货通知
                            var PB_DeliveryNoticeReturnModel = await Db.GetIQueryable<PB_DeliveryNotice>().Where(m => m.Id == cfg.BillId && m.Type == 2).FirstOrDefaultAsync();
                            if (PB_DeliveryNoticeReturnModel != null)
                            {

                                //有工作流的
                                JArray jarr = JArray.Parse(cfg.Content);
                                JObject jobj = JObject.Parse(jarr[cfg.Level].ToString());
                                string txt = jobj["StartState"]["Name"].ToString();
                                string rid = jobj["Assignees"]["RoleId"].ToString();
                                string btn = jobj["Action"]["Name"].ToString();
                                JArray usr = JArray.Parse(jobj["Assignees"]["UserId"].ToString());
                                if (usr.Count() > 0)
                                {
                                    foreach (var item in usr)
                                    {
                                        string _u = item.ToString();
                                        if (_u.Equals(_operator.UserId))
                                        {
                                            Base_OverFlowTask temp = new Base_OverFlowTask();
                                            temp.Id = cfg.Id;
                                            temp.BillId = cfg.BillId;
                                            temp.BillType = cfg.BillType;
                                            var Base_EnumItemModel = await Db.GetIQueryable<Base_EnumItem>().Where(m => m.Code == cfg.BillType && m.EnumCode == "BillType").FirstOrDefaultAsync();
                                            string aa = "";
                                            if (Base_EnumItemModel != null)
                                            {
                                                aa = Base_EnumItemModel.Name;
                                            }
                                            temp.Title = DateTime.Now.ToString("g") + aa + "(" + txt + ")";
                                            temp.BtnName = btn;
                                            temp.ProjectName = PB_DeliveryNoticeReturnModel.ProjectName;
                                            temp.Rule = 0;
                                            result.Add(temp);
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    //角色
                                    if (_operator.Property.RoleIdList.Contains(rid))
                                    {
                                        Base_OverFlowTask temp = new Base_OverFlowTask();
                                        temp.Id = cfg.Id;
                                        temp.BillId = cfg.BillId;
                                        temp.BillType = cfg.BillType;
                                        var Base_EnumItemModel = await Db.GetIQueryable<Base_EnumItem>().Where(m => m.Code == cfg.BillType && m.EnumCode == "BillType").FirstOrDefaultAsync();
                                        string aa = "";
                                        if (Base_EnumItemModel != null)
                                        {
                                            aa = Base_EnumItemModel.Name;
                                        }
                                        temp.Title = DateTime.Now.ToString("g") + aa + "(" + cfg.Title + ")";
                                        temp.ProjectName = PB_DeliveryNoticeReturnModel.ProjectName;
                                        temp.Rule = 0;
                                        result.Add(temp);
                                    }
                                }

                            }
                            #endregion
                            break;
                        case "DesignList":
                            #region 设计清单
                            var PB_DesignListModel = await Db.GetIQueryable<PB_DesignList>().Where(m => m.Id == cfg.BillId).FirstOrDefaultAsync();
                            var ProjectName = "";
                            var Project = await Db.GetIQueryable<PB_Project>().Where(m => m.Id == PB_DesignListModel.ProjectId).FirstOrDefaultAsync();
                            ProjectName = Project.Name;
                            var BuliNumber = "";
                            var BuliNumberNo = await Db.GetIQueryable<PB_Buildingnumber>().Where(m => m.Id == PB_DesignListModel.BuildingNumberID).FirstOrDefaultAsync();
                            BuliNumber = BuliNumberNo.Buildingnumber;
                            if (PB_DesignListModel != null)
                            {

                                //有工作流的
                                JArray jarr = JArray.Parse(cfg.Content);
                                JObject jobj = JObject.Parse(jarr[cfg.Level].ToString());
                                string txt = jobj["StartState"]["Name"].ToString();
                                string rid = jobj["Assignees"]["RoleId"].ToString();
                                string btn = jobj["Action"]["Name"].ToString();
                                JArray usr = JArray.Parse(jobj["Assignees"]["UserId"].ToString());
                                if (usr.Count() > 0)
                                {
                                    foreach (var item in usr)
                                    {
                                        string _u = item.ToString();
                                        if (_u.Equals(_operator.UserId))
                                        {
                                            Base_OverFlowTask temp = new Base_OverFlowTask();
                                            temp.Id = cfg.Id;
                                            temp.BillId = cfg.BillId;
                                            temp.BillType = cfg.BillType;
                                            var Base_EnumItemModel = await Db.GetIQueryable<Base_EnumItem>().Where(m => m.Code == cfg.BillType && m.EnumCode == "BillType").FirstOrDefaultAsync();
                                            string aa = "";
                                            if (Base_EnumItemModel != null)
                                            {
                                                aa = Base_EnumItemModel.Name;
                                            }
                                            temp.Title = DateTime.Now.ToString("g") + aa + "(" + txt + ")";
                                            temp.BtnName = btn;
                                            temp.ProjectName = ProjectName + BuliNumber;
                                            temp.Rule = 0;
                                            temp.Level = 0;
                                            result.Add(temp);
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    //角色
                                    if (_operator.Property.RoleIdList.Contains(rid))
                                    {
                                        Base_OverFlowTask temp = new Base_OverFlowTask();
                                        temp.Id = cfg.Id;
                                        temp.BillId = cfg.BillId;
                                        temp.BillType = cfg.BillType;
                                        var Base_EnumItemModel = await Db.GetIQueryable<Base_EnumItem>().Where(m => m.Code == cfg.BillType && m.EnumCode == "BillType").FirstOrDefaultAsync();
                                        string aa = "";
                                        if (Base_EnumItemModel != null)
                                        {
                                            aa = Base_EnumItemModel.Name;
                                        }
                                        temp.Title = DateTime.Now.ToString("g") + aa + "(" + cfg.Title + ")";
                                        temp.ProjectName = ProjectName + BuliNumber;
                                        temp.Rule = 0;
                                        temp.Level = 0;
                                        result.Add(temp);
                                    }
                                }

                            }
                            #endregion
                            break;
                        case "InsertDesignList":
                            #region 补加单
                            var PB_DesignListInsertModel = await Db.GetIQueryable<PB_DesignList>().Where(m => m.Id == cfg.BillId).FirstOrDefaultAsync();
                            var ProjectNameInsert = "";
                            var ProjectInsert = await Db.GetIQueryable<PB_Project>().Where(m => m.Id == PB_DesignListInsertModel.ProjectId).FirstOrDefaultAsync();
                            ProjectNameInsert = ProjectInsert.Name;
                            var BuliNumberInsert = "";
                            var BuliNumberNoInsert = await Db.GetIQueryable<PB_Buildingnumber>().Where(m => m.Id == PB_DesignListInsertModel.BuildingNumberID).FirstOrDefaultAsync();
                            BuliNumberInsert = BuliNumberNoInsert.Buildingnumber;
                            if (PB_DesignListInsertModel != null)
                            {

                                //有工作流的
                                JArray jarr = JArray.Parse(cfg.Content);
                                JObject jobj = JObject.Parse(jarr[cfg.Level].ToString());
                                string txt = jobj["StartState"]["Name"].ToString();
                                string rid = jobj["Assignees"]["RoleId"].ToString();
                                string btn = jobj["Action"]["Name"].ToString();
                                JArray usr = JArray.Parse(jobj["Assignees"]["UserId"].ToString());
                                if (usr.Count() > 0)
                                {
                                    foreach (var item in usr)
                                    {
                                        string _u = item.ToString();
                                        if (_u.Equals(_operator.UserId))
                                        {
                                            Base_OverFlowTask temp = new Base_OverFlowTask();
                                            temp.Id = cfg.Id;
                                            temp.BillId = cfg.BillId;
                                            temp.BillType = cfg.BillType;
                                            var Base_EnumItemModel = await Db.GetIQueryable<Base_EnumItem>().Where(m => m.Code == cfg.BillType && m.EnumCode == "BillType").FirstOrDefaultAsync();
                                            string aa = "";
                                            if (Base_EnumItemModel != null)
                                            {
                                                aa = Base_EnumItemModel.Name;
                                            }
                                            temp.Title = DateTime.Now.ToString("g") + aa + "(" + txt + ")";
                                            temp.BtnName = btn;
                                            temp.ProjectName = ProjectNameInsert + BuliNumberInsert;
                                            temp.Rule = 0;
                                            result.Add(temp);
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    //角色
                                    if (_operator.Property.RoleIdList.Contains(rid))
                                    {
                                        Base_OverFlowTask temp = new Base_OverFlowTask();
                                        temp.Id = cfg.Id;
                                        temp.BillId = cfg.BillId;
                                        temp.BillType = cfg.BillType;
                                        var Base_EnumItemModel = await Db.GetIQueryable<Base_EnumItem>().Where(m => m.Code == cfg.BillType && m.EnumCode == "BillType").FirstOrDefaultAsync();
                                        string aa = "";
                                        if (Base_EnumItemModel != null)
                                        {
                                            aa = Base_EnumItemModel.Name;
                                        }
                                        temp.Title = DateTime.Now.ToString("g") + aa + "(" + cfg.Title + ")";
                                        temp.ProjectName = ProjectNameInsert + BuliNumberInsert;
                                        temp.Rule = 0;
                                        result.Add(temp);
                                    }
                                }

                            }
                            #endregion
                            break;
                        case "ChangeDesignList":
                            #region 变更单
                            #endregion
                            break;
                    }

                }
            }
            return result;
        }

        public async Task<OverFlowTaskDTO> GetTaskInfoDataAsync(OverFlowInputDTO input)
        {
            OverFlowTaskDTO result = new OverFlowTaskDTO();
            //获取表单类型
            var task = await Db.GetListBySqlAsync<Base_OverFlowBill>("SELECT * FROM Base_OverFlowTask WHERE BillId='" + input.BillId + "'");
            if (task.Count() == 0)
            {
                throw new BusException("无详情数据！", 404);
            }
            StringBuilder sql = new StringBuilder();
            List<OverFlowTaskItem> list = new List<OverFlowTaskItem>();
            List<OverFlowTaskItem> subs = new List<OverFlowTaskItem>();
            switch (task[0].BillType)
            {
                case "DeliveryPlan":
                    //发货计划
                    #region 查询主表
                    sql.Append("SELECT ");
                    sql.Append("b.NAME FactoryName,");
                    sql.Append("c.NAME DeptName,");
                    sql.Append("a.PlannedDate,");
                    sql.Append("a.Closingdate,");
                    sql.Append("ifnull( a.Remarks, '暂无' ) Remarks ");
                    sql.Append("FROM ");
                    sql.Append("PB_DeliveryPlan a ");
                    sql.Append("LEFT JOIN Base_Department b ON a.DeptId = b.Id ");
                    sql.Append("LEFT JOIN Base_Department c ON a.DepartmentId = c.Id ");
                    sql.Append("WHERE a.Id='" + input.BillId + "'");

                    var billPlan = await Db.GetListBySqlAsync<DevilveryPlanDTO>(sql.ToString());
                    list.Add(new OverFlowTaskItem() { Key = "工厂名称", Value = billPlan[0].FactoryName });
                    list.Add(new OverFlowTaskItem() { Key = "主管部门", Value = billPlan[0].DeptName });
                    list.Add(new OverFlowTaskItem() { Key = "计划日期", Value = Convert.ToDateTime(billPlan[0].PlannedDate).ToString("yyyy-MM-dd") });
                    list.Add(new OverFlowTaskItem() { Key = "截至日期", Value = Convert.ToDateTime(billPlan[0].Closingdate).ToString("yyyy-MM-dd") });
                    list.Add(new OverFlowTaskItem() { Key = "备注", Value = billPlan[0].Remarks });
                    #endregion

                    #region 查询子表
                    sql.Clear();
                    sql.Append("SELECT ");
                    sql.Append("b.`Name` ProjectName,");
                    sql.Append("c.Buildingnumber,");
                    sql.Append("a.ShipmentQuantity,");
                    sql.Append("a.ShipmentWight ");
                    sql.Append("FROM ");
                    sql.Append("PB_DeliveryPlanDetails a ");
                    sql.Append("LEFT JOIN PB_Project b ON a.ProjectId = b.Id ");
                    sql.Append("LEFT JOIN PB_Buildingnumber c ON a.BuildingId = c.Id ");
                    sql.Append("WHERE ");
                    sql.Append("a.ParentId='" + input.BillId + "'");

                    var planDetail = await Db.GetListBySqlAsync<DevilveryPlanSubDTO>(sql.ToString());
                    foreach (var detail in planDetail)
                    {
                        subs.Add(new OverFlowTaskItem() { Key = "楼号：" + detail.Buildingnumber + "  发货数量：" + detail.ShipmentQuantity + "  发货重量：" + detail.ShipmentWight, Value = detail.ProjectName });
                    }
                    #endregion
                    break;
                case "MobilizationNotice":
                    //进场通知
                    #region 查询主表
                    sql.Append("SELECT ");
                    sql.Append("* ");
                    sql.Append("FROM ");
                    sql.Append("PB_MobilizationNotice ");
                    sql.Append("WHERE Id='" + input.BillId + "'");
                    var billMob = await Db.GetListBySqlAsync<PB_MobilizationNotice>(sql.ToString());
                    list.Add(new OverFlowTaskItem() { Key = "表单类型", Value = "进场通知" });
                    list.Add(new OverFlowTaskItem() { Key = "项目名称", Value = billMob[0].ProjectName });
                    list.Add(new OverFlowTaskItem() { Key = "合同编号", Value = billMob[0].ContractNo });
                    list.Add(new OverFlowTaskItem() { Key = "下发日期", Value = Convert.ToDateTime(billMob[0].IssueDate).ToString("yyyy-MM-dd") });
                    list.Add(new OverFlowTaskItem() { Key = "业务类型", Value = billMob[0].TypeId });
                    list.Add(new OverFlowTaskItem() { Key = "购销单位", Value = billMob[0].SaleUnitName });
                    list.Add(new OverFlowTaskItem() { Key = "合作单位", Value = billMob[0].CooperativeUnitName });
                    list.Add(new OverFlowTaskItem() { Key = "签订单位", Value = billMob[0].SignUnitName });
                    list.Add(new OverFlowTaskItem() { Key = "备注", Value = billMob[0].Remark });
                    #endregion
                    #region 查询子表
                    sql.Clear();
                    sql.Append("SELECT ");
                    sql.Append("a.Id,");
                    sql.Append("a.ParentId,");
                    sql.Append("b.Buildingnumber BuildingnumberId,");
                    sql.Append("a.MobilizationTime,");
                    sql.Append("ifnull(a.ProductModel,'暂未提供') ProductModel,");
                    sql.Append("a.Remarks,");
                    sql.Append("a.Deleted,");
                    sql.Append("a.CreateTime,");
                    sql.Append("a.CreatorId,");
                    sql.Append("a.CreatorName,");
                    sql.Append("a.BuildingNo ");
                    sql.Append("FROM ");
                    sql.Append("PB_MobilzationItem a ");
                    sql.Append("LEFT JOIN PB_Buildingnumber b ON a.BuildingnumberId = b.Id ");
                    sql.Append("WHERE ");
                    sql.Append("a.ParentId='" + input.BillId + "'");
                    var MobilzationItem = await Db.GetListBySqlAsync<PB_MobilzationItem>(sql.ToString());
                    foreach (var detail in MobilzationItem)
                    {
                        subs.Add(new OverFlowTaskItem() { Key = "楼号：" + detail.BuildingnumberId + "  进场时间：" + detail.MobilizationTime, Value = "产品型号:" + detail.ProductModel });
                    }
                    #endregion
                    break;
                case "DeliveryNotice":
                    //发货通知
                    #region 查询主表
                    sql.Append("SELECT ");
                    sql.Append("* ");
                    sql.Append("FROM ");
                    sql.Append("PB_DeliveryNotice ");
                    sql.Append("WHERE Id='" + input.BillId + "'");
                    //创建发货通知数据
                    var DeliveryNoticeMod = await Db.GetListBySqlAsync<PB_DeliveryNotice>(sql.ToString());
                    list.Add(new OverFlowTaskItem() { Key = "表单类型", Value = "发货通知" });
                    list.Add(new OverFlowTaskItem() { Key = "项目名称", Value = DeliveryNoticeMod[0].ProjectName });
                    list.Add(new OverFlowTaskItem() { Key = "合同编号", Value = DeliveryNoticeMod[0].ContractNo });
                    list.Add(new OverFlowTaskItem() { Key = "下发日期", Value = Convert.ToDateTime(DeliveryNoticeMod[0].IssueDate).ToString("yyyy-MM-dd") });
                    list.Add(new OverFlowTaskItem() { Key = "购销单位", Value = DeliveryNoticeMod[0].SaleUnitName });
                    list.Add(new OverFlowTaskItem() { Key = "合作单位", Value = DeliveryNoticeMod[0].CooperativeUnitName });
                    list.Add(new OverFlowTaskItem() { Key = "签订单位", Value = DeliveryNoticeMod[0].SignUnitName });
                    list.Add(new OverFlowTaskItem() { Key = "业务类型", Value = DeliveryNoticeMod[0].TypeId });
                    list.Add(new OverFlowTaskItem() { Key = "运费详情", Value = DeliveryNoticeMod[0].FreightDetails });
                    list.Add(new OverFlowTaskItem() { Key = "发货要求", Value = DeliveryNoticeMod[0].Remark });
                    list.Add(new OverFlowTaskItem() { Key = "送货地址", Value = DeliveryNoticeMod[0].Address });
                    list.Add(new OverFlowTaskItem() { Key = "结算方式", Value = DeliveryNoticeMod[0].Remarks });
                    #endregion
                    #region //组装发货通知单子数据
                    sql.Clear();
                    sql.Append("SELECT ");
                    sql.Append("a.Id,");
                    sql.Append("a.ParentId,");
                    sql.Append("BuildingNo,");
                    sql.Append("InDate,");
                    sql.Append("Requirement ");
                    sql.Append("FROM ");
                    sql.Append("PB_DeliveryNoticeItem a ");
                    sql.Append("WHERE ");
                    sql.Append("a.ParentId='" + input.BillId + "'");
                    var DeliveryNoticeItem = await Db.GetListBySqlAsync<PB_DeliveryNoticeItem>(sql.ToString());
                    foreach (var DeliveryNoticeItemDetali in DeliveryNoticeItem)
                    {
                        subs.Add(new OverFlowTaskItem() { Key = "楼号：" + DeliveryNoticeItemDetali.BuildingNo + "  下发日期：" + DeliveryNoticeItemDetali.InDate, Value = "产品要求:" + DeliveryNoticeItemDetali.Requirement });
                    }
                    #endregion
                    break;
                case "ReturnNotice":
                    //发货通知
                    #region 查询主表
                    sql.Append("SELECT ");
                    sql.Append("* ");
                    sql.Append("FROM ");
                    sql.Append("PB_DeliveryNotice ");
                    sql.Append("WHERE Id='" + input.BillId + "'");
                    //创建发货通知数据
                    var ReturnNotice = await Db.GetListBySqlAsync<PB_DeliveryNotice>(sql.ToString());
                    list.Add(new OverFlowTaskItem() { Key = "表单类型", Value = "回货通知" });
                    list.Add(new OverFlowTaskItem() { Key = "项目名称", Value = ReturnNotice[0].ProjectName });
                    list.Add(new OverFlowTaskItem() { Key = "收货人姓名", Value = ReturnNotice[0].ConsigneeName });
                    list.Add(new OverFlowTaskItem() { Key = "联系方式", Value = ReturnNotice[0].ConsigneeTelephone });
                    list.Add(new OverFlowTaskItem() { Key = "回货日期", Value = ReturnNotice[0].IssueDate.ToString() });
                    list.Add(new OverFlowTaskItem() { Key = "回货吨位", Value = ReturnNotice[0].ReturnTonnage.ToString() });
                    list.Add(new OverFlowTaskItem() { Key = "摘要", Value = ReturnNotice[0].Abstract });
                    #endregion
                    break;
                case "DesignList":
                    sql.AppendLine("SELECT");
                    sql.AppendLine("PB_Project.`Name` ProjectAbbreviation,");
                    sql.AppendLine("PB_Buildingnumber.Buildingnumber,");
                    sql.AppendLine("PB_DesignList.*,");
                    sql.AppendLine("PB_DesignListFile.DetailedFile");
                    sql.AppendLine("FROM");
                    sql.AppendLine("PB_DesignList");
                    sql.AppendLine("LEFT JOIN PB_Project ON PB_DesignList.ProjectId = PB_Project.Id");
                    sql.AppendLine("LEFT JOIN PB_Buildingnumber ON PB_DesignList.BuildingNumberID = PB_Buildingnumber.Id");
                    sql.AppendLine("LEFT JOIN PB_DesignListFile ON PB_DesignListFile.DesignId=PB_DesignList.Id");
                    sql.AppendLine("AND PB_DesignListFile.Type=0");
                    sql.AppendLine("WHERE");
                    sql.AppendLine("PB_DesignList.Id = '" + input.BillId + "'");

                    sql.AppendLine("Order by PB_DesignListFile.CreateTime desc   LIMIT  1");
                    //创建设计清单结果集
                    var PB_DesignList = await Db.GetListBySqlAsync<DingDesignListDto>(sql.ToString());
                    list.Add(new OverFlowTaskItem() { Key = "表单类型", Value = "设计清单" });
                    list.Add(new OverFlowTaskItem() { Key = "项目名称", Value = PB_DesignList[0].ProjectAbbreviation + PB_DesignList[0].Buildingnumber });
                    list.Add(new OverFlowTaskItem() { Key = "合同编号", Value = PB_DesignList[0].ContractNo });
                    list.Add(new OverFlowTaskItem() { Key = "设计清单名称", Value = PB_DesignList[0].Name });
                    list.Add(new OverFlowTaskItem() { Key = "工程名称", Value = PB_DesignList[0].ProjectName });
                    list.Add(new OverFlowTaskItem() { Key = "下单日期", Value = PB_DesignList[0].OrderDate.ToString() });
                    list.Add(new OverFlowTaskItem() { Key = "附件", Value = PB_DesignList[0].DetailedFile });
                    break;
                case "InsertDesignList":
                    sql.AppendLine("SELECT");
                    sql.AppendLine("PB_Project.`Name` ProjectAbbreviation,");
                    sql.AppendLine("PB_Buildingnumber.Buildingnumber,");
                    sql.AppendLine("PB_DesignList.*,");
                    sql.AppendLine("PB_DesignListFile.DetailedFile");
                    sql.AppendLine("FROM");
                    sql.AppendLine("PB_DesignList");
                    sql.AppendLine("LEFT JOIN PB_Project ON PB_DesignList.ProjectId = PB_Project.Id");
                    sql.AppendLine("LEFT JOIN PB_Buildingnumber ON PB_DesignList.BuildingNumberID = PB_Buildingnumber.Id");
                    sql.AppendLine("LEFT JOIN PB_DesignListFile ON PB_DesignListFile.DesignId=PB_DesignList.Id AND PB_DesignListFile.Type=1");
                    sql.AppendLine("WHERE");
                    sql.AppendLine("PB_DesignList.Id = '" + input.BillId + "'");
                    sql.AppendLine("Order by PB_DesignListFile.CreateTime desc   LIMIT  1");
                    //创建设计清单结果集
                    var PB_DesignListInsert = await Db.GetListBySqlAsync<DingDesignListDto>(sql.ToString());
                    list.Add(new OverFlowTaskItem() { Key = "表单类型", Value = "补加" });
                    list.Add(new OverFlowTaskItem() { Key = "项目名称", Value = PB_DesignListInsert[0].ProjectAbbreviation + PB_DesignListInsert[0].Buildingnumber });
                    list.Add(new OverFlowTaskItem() { Key = "合同编号", Value = PB_DesignListInsert[0].ContractNo });
                    list.Add(new OverFlowTaskItem() { Key = "设计清单名称", Value = PB_DesignListInsert[0].Name });
                    list.Add(new OverFlowTaskItem() { Key = "工程名称", Value = PB_DesignListInsert[0].ProjectName });
                    list.Add(new OverFlowTaskItem() { Key = "下单日期", Value = PB_DesignListInsert[0].OrderDate.ToString() });
                    list.Add(new OverFlowTaskItem() { Key = "附件", Value = PB_DesignListInsert[0].DetailedFile });
                    break;
                case "ChangeDesignList":
                    sql.AppendLine("SELECT");
                    sql.AppendLine("PB_Project.`Name` ProjectAbbreviation,");
                    sql.AppendLine("PB_Buildingnumber.Buildingnumber,");
                    sql.AppendLine("PB_DesignList.*,");
                    sql.AppendLine("PB_DesignListFile.DetailedFile");
                    sql.AppendLine("FROM");
                    sql.AppendLine("PB_DesignList");
                    sql.AppendLine("LEFT JOIN PB_Project ON PB_DesignList.ProjectId = PB_Project.Id");
                    sql.AppendLine("LEFT JOIN PB_Buildingnumber ON PB_DesignList.BuildingNumberID = PB_Buildingnumber.Id");
                    sql.AppendLine("LEFT JOIN PB_DesignListFile ON PB_DesignListFile.DesignId=PB_DesignList.Id AND PB_DesignListFile.Type=2");
                    sql.AppendLine("WHERE");
                    sql.AppendLine("PB_DesignList.Id = '" + input.BillId + "'");
                    sql.AppendLine("Order by PB_DesignListFile.CreateTime desc   LIMIT  1");
                    //设计清单变更单
                    var PB_DesignListChange = await Db.GetListBySqlAsync<DingDesignListDto>(sql.ToString());
                    list.Add(new OverFlowTaskItem() { Key = "清单类型", Value = "变更" });
                    list.Add(new OverFlowTaskItem() { Key = "项目名称", Value = PB_DesignListChange[0].ProjectAbbreviation + PB_DesignListChange[0].Buildingnumber });
                    list.Add(new OverFlowTaskItem() { Key = "合同编号", Value = PB_DesignListChange[0].ContractNo });
                    list.Add(new OverFlowTaskItem() { Key = "设计清单名称", Value = PB_DesignListChange[0].Name });
                    list.Add(new OverFlowTaskItem() { Key = "工程名称", Value = PB_DesignListChange[0].ProjectName });
                    list.Add(new OverFlowTaskItem() { Key = "下单日期", Value = PB_DesignListChange[0].OrderDate.ToString() });
                    list.Add(new OverFlowTaskItem() { Key = "附件", Value = PB_DesignListChange[0].DetailedFile });
                    break;
                default:
                    break;
            }
            var flow = await Db.GetListBySqlAsync<Base_OverFlow>("select * from Base_OverFlow where Id='" + input.Id + "'");
            JArray farr = JArray.Parse(flow[0].Content);
            List<Approver> chks = new List<Approver>();
            int _index = 0;
            //获取下级推送人信息
            SendInfo sendInfo = new SendInfo();
            foreach (var f in farr)
            {
                JObject _obj = JObject.Parse(f.ToString());
                JArray usr = JArray.Parse(_obj["Assignees"]["UserId"].ToString());
                string uids = "";
                string dingId = "";
                string dingName = "";
                if (usr.Count() > 0)
                {
                    //员工
                    foreach (var u in usr)
                    {
                        uids += "'" + u + "',";
                    }
                    var emps = await Db.GetListBySqlAsync<Base_User>("SELECT * FROM Base_User WHERE Id IN (" + uids.Substring(0, uids.Length - 1) + ")");
                    foreach (var e in emps)
                    {
                        dingId += e.DingUserId + ",";
                        dingName += e.RealName + " ";
                    }
                }
                else
                {
                    //角色
                    string rid = _obj["Assignees"]["RoleId"].ToString();
                    var emps = await Db.GetListBySqlAsync<Base_User>("SELECT * FROM Base_User WHERE UserPost = (SELECT RoleName FROM Base_Role WHERE Id='" + rid + "')");
                    foreach (var e in emps)
                    {
                        dingId += e.DingUserId + ",";
                        dingName += e.RealName + " ";
                    }
                }
                Approver _app = new Approver();
                _app.Id = dingId;
                _app.Name = dingName;
                _app.Des = _obj["StartState"]["Name"].ToString();
                _app.iShow = _index == (farr.Count - 1) ? false : true;
                _app.Style = task[0].Level == _index ? "" : "-def";
                if (task[0].Level + 1 == _index)
                {

                    sendInfo.Users = dingId.Split(',').ToList();
                }
                if (task[0].Level == _index)
                {
                    sendInfo.Text = _obj["ToState"]["Name"].ToString();
                }
                chks.Add(_app);
                _index++;
            }
            result.Contents = list;
            result.Subs = subs;
            result.Chks = chks;
            result.Send = sendInfo;
            return result;
        }

        public async Task<Base_OverFlow> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        [DataRepeatValidate(new string[] { "BillType" }, new string[] { "表单类型" })]
        public async Task AddDataAsync(FP_FlowProcessDTO data)
        {
            Base_OverFlow overFlow = new Base_OverFlow();
            overFlow.Id = data.Id;
            overFlow.CreatorId = data.CreatorId;
            overFlow.CreateTime = data.CreateTime;
            overFlow.Deleted = false;
            overFlow.Title = data.Title;
            overFlow.Level = data.Level;
            overFlow.BillType = data.BillType;
            List<FP_FlowProcess> flowProcessItems = new List<FP_FlowProcess>();
            foreach (var item in data.FlowProcess)
            {
                FP_FlowProcess fP_FlowProcess = new FP_FlowProcess();
                fP_FlowProcess.StartState = new FlowProcessItem() { Id = IdHelper.GetId(), Name = item.StartState };
                fP_FlowProcess.Action = new FlowProcessItem() { Id = IdHelper.GetId(), Name = item.Action };
                fP_FlowProcess.ToState = new FlowProcessItem() { Id = IdHelper.GetId(), Name = item.ToState };
                fP_FlowProcess.Assignees = new AssigneeItem() { RoleId = item.Rule, UserId = item.User, PersonId = item.UserPerson };
                fP_FlowProcess.ToDept = new FlowProcessItem() { Id = item.DeptId, Name = item.DeptName };
                fP_FlowProcess.UserPosts = new UserPost() { UserPosts = item.UserPosts };
                List<Entity.FP.PushpersonnelList> renYuanLists = new List<Entity.FP.PushpersonnelList>();
                foreach (var item1 in item.RenYuanList)
                {
                    Entity.FP.PushpersonnelList renYuanList = new Entity.FP.PushpersonnelList();
                    renYuanList.Id = item1.Id;
                    renYuanList.Name = item1.Name;
                    renYuanLists.Add(renYuanList);
                }
                fP_FlowProcess.RenYuanList = renYuanLists;
                flowProcessItems.Add(fP_FlowProcess);
            }
            overFlow.Content = flowProcessItems.ToJson();
            await InsertAsync(overFlow);
        }

        public async Task UpdateDataAsync(FP_FlowProcessDTO data)
        {
            Base_OverFlow overFlow = new Base_OverFlow();
            overFlow.Id = data.Id;
            overFlow.CreatorId = data.CreatorId;
            overFlow.CreateTime = data.CreateTime;
            overFlow.Deleted = data.Deleted;
            overFlow.Title = data.Title;
            overFlow.Level = data.Level;
            overFlow.BillType = data.BillType;
            List<FP_FlowProcess> flowProcessItems = new List<FP_FlowProcess>();
            foreach (var item in data.FlowProcess)
            {
                FP_FlowProcess fP_FlowProcess = new FP_FlowProcess();
                fP_FlowProcess.StartState = new FlowProcessItem() { Id = IdHelper.GetId(), Name = item.StartState };
                fP_FlowProcess.Action = new FlowProcessItem() { Id = IdHelper.GetId(), Name = item.Action };
                fP_FlowProcess.ToState = new FlowProcessItem() { Id = IdHelper.GetId(), Name = item.ToState };
                fP_FlowProcess.Assignees = new AssigneeItem() { RoleId = item.Rule, UserId = item.User, PersonId = item.UserPerson };
                fP_FlowProcess.ToDept = new FlowProcessItem() { Id = item.DeptId, Name = item.DeptName };
                fP_FlowProcess.UserPosts = new UserPost() { UserPosts = item.UserPosts };
                List<Entity.FP.PushpersonnelList> renYuanLists = new List<Entity.FP.PushpersonnelList>();
                foreach (var item1 in item.RenYuanList)
                {
                    Entity.FP.PushpersonnelList renYuanList = new Entity.FP.PushpersonnelList();
                    renYuanList.Id = item1.Id;
                    renYuanList.Name = item1.Name;
                    renYuanLists.Add(renYuanList);
                }
                fP_FlowProcess.RenYuanList = renYuanLists;
                flowProcessItems.Add(fP_FlowProcess);
            }
            overFlow.Content = flowProcessItems.ToJson();
            await UpdateAsync(overFlow);
        }

        [OverFlowDelete]
        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }


        public async Task ApprovedDataAsync(OverFlowInputDTO input)
        {
            //定义跳转路径

            //先获取当前的级别
            var task = await Db.GetListBySqlAsync<Base_OverFlowBill>("SELECT * FROM Base_OverFlowTask WHERE BillId='" + input.BillId + "'");
            if (task.Count==0)
            {
                throw new BusException("当前单据已审批完成!");
            }
            int curlevel = (int)task[0].Level;
            if (curlevel == 0)
            {
                string sql = "delete from  Base_OverFlowLog where BillId='" + input.BillId + "' AND  OverFlowId='" + input.Id + "'";
                await Db.ExecuteSqlAsync(sql);
            }
            //获取工作流中设置的全部级别
            var flow = await Db.GetListBySqlAsync<Base_OverFlow>("select * from Base_OverFlow where Id='" + input.Id + "'");
            if (flow.Count == 0)
            {
                throw new BusException("");
            }

            JArray farr = JArray.Parse(flow[0].Content);
            JObject currobj = JObject.Parse(farr[curlevel].ToString());
            Base_OverFlowLog log = new Base_OverFlowLog();
            if (!currobj["Assignees"]["UserId"].ToString().Contains(_operator.UserId))
            {
                throw new BusException("您的审批已通过!");
            }
            //如果是最终级别
            if (farr.Count() == curlevel + 1)
            {

                //删除任务表
                await Db.ExecuteSqlAsync("DELETE FROM Base_OverFlowTask WHERE BillId='" + input.BillId + "'");
                //插入log表
                log.Id = IdHelper.GetId();
                log.CreatorId = _operator.UserId;
                log.CreateTime = DateTime.Now;
                log.OverFlowId = input.Id;
                log.OverFlowDetailID = currobj["StartState"]["Id"].ToString();
                log.BillType = task[0].BillType;
                log.BillId = input.BillId;
                log.AssignTo = "";
                log.Remark = currobj["UserPosts"]["UserPosts"].ToString();
                await Db.InsertAsync<Base_OverFlowLog>(log);
            }
            else
            {
                JObject nextobj = JObject.Parse(farr[curlevel + 1].ToString());
                //更新任务表
                await Db.ExecuteSqlAsync("UPDATE Base_OverFlowTask SET Level=" + (curlevel + 1) + " WHERE BillId='" + input.BillId + "'");
                //插入log表
                log.Id = IdHelper.GetId();
                log.CreatorId = _operator.UserId;
                log.CreateTime = DateTime.Now;
                log.OverFlowId = input.Id;
                log.OverFlowDetailID = currobj["StartState"]["Id"].ToString();
                log.BillType = task[0].BillType;
                log.BillId = input.BillId;
                log.AssignTo = nextobj["StartState"]["Id"].ToString();
                log.Remark = currobj["UserPosts"]["UserPosts"].ToString();
                await Db.InsertAsync<Base_OverFlowLog>(log);

            }
            try
            {
                //进行switch选择 根据选择的工作流类型选择不同的数据
                List<Base_OverFlowTaskDtos> flowS = Db.GetListBySqlAsync<Base_OverFlowTaskDtos>("select BillId,BillType from Base_OverFlowTask where BillId='" + input.BillId + "'").Result;
                if (flowS.Count > 0)
                {


                    //审批人跳转页面
                    Util.Helper.DingInputDTO result = new Util.Helper.DingInputDTO();
                    //申请人跳转页面
                    Util.Helper.DingInputDTO Applicantresuit = new Util.Helper.DingInputDTO();
                    //抄送人跳转列表
                    Util.Helper.DingInputDTO CClist = new Util.Helper.DingInputDTO();

                    switch (flowS[0].BillType)
                    {
                        case "DeliveryNotice"://发货通知
                            //获取审批人
                            List<string> UserList = new List<string>();
                            //获取申请人
                            List<string> applicantList = new List<string>();
                            StringBuilder DeliveryNoticeSql = new StringBuilder();
                            DeliveryNoticeSql.Append("Select ");
                            DeliveryNoticeSql.Append("b.Content,");
                            DeliveryNoticeSql.Append("ifnull(c.LEVEL,0) LEVEL ");
                            DeliveryNoticeSql.Append("FROM ");
                            DeliveryNoticeSql.Append("PB_DeliveryNotice a ");
                            DeliveryNoticeSql.Append("LEFT JOIN Base_OverFlow b ON a.WorkFlow = b.Id  ");
                            DeliveryNoticeSql.Append("LEFT JOIN Base_OverFlowTask c ON a.Id = c.BillId ");
                            DeliveryNoticeSql.Append("WHERE ");
                            DeliveryNoticeSql.Append("a.Id='" + input.BillId + "'");
                            //推送创建发货通知单用户钉钉信息
                            string uidss = "";
                            var flowMobs = await Db.GetListBySqlAsync<CurrentOverFlowDTO>(DeliveryNoticeSql.ToString());
                            if (flowMobs.Count==0)
                            {
                                throw new BusException("当前单据已审批完成!");
                            }
                            JArray farrMobs = JArray.Parse(flowMobs[0].Content);
                            JObject _objMobs = JObject.Parse(farrMobs[flowMobs[0].Level].ToString());
                            //推送下一级人员id
                            JArray usrMobs = JArray.Parse(_objMobs["Assignees"]["UserId"].ToString());
                            JToken[] liststr = _objMobs["Assignees"]["UserId"].ToArray();
                            if (liststr.Length > 0)
                            {
                                foreach (var udingid in liststr)
                                {
                                    string userid = udingid.ToString();
                                    var base_user = await Db.GetIQueryable<Base_User>().Where(m => m.Id == userid).FirstOrDefaultAsync();
                                    if (base_user != null)
                                    {
                                        UserList.Add(base_user.DingUserId);
                                    }
                                }
                            }
                            else
                            {

                                string rid = _objMobs["Assignees"]["RoleId"].ToString();
                                var emps = await Db.GetListBySqlAsync<Base_User>("SELECT * FROM Base_User WHERE UserPost = (SELECT RoleName FROM Base_Role WHERE Id='" + rid + "')");
                                foreach (var e in emps)
                                {
                                    UserList.Add(e.DingUserId);
                                }

                            }

                            var listusers = await Db.GetIQueryable<PB_DeliveryNotice>().Where(m => m.Id == input.BillId).FirstOrDefaultAsync();
                            string DeliveryNoticeLou = "";
                            var list = Db.GetIQueryable<PB_DeliveryNoticeItem>().Where(m => m.ParentId == input.BillId).ToList();
                            foreach (var k in list)
                            {
                                DeliveryNoticeLou += k.BuildingNo;
                            }
                            //审批人跳转路径
                            var Base_OverFlowLogTop1 = await Db.GetIQueryable<Base_OverFlowLog>().Where(m => m.BillId == input.BillId).OrderByDescending(m => m.CreateTime).FirstOrDefaultAsync();
                            var baseuser = await Db.GetIQueryable<Base_User>().Where(m => m.Id == Base_OverFlowLogTop1.CreatorId).FirstOrDefaultAsync();
                            if (farr.Count() == curlevel + 1)
                            {
                                result.Str = $"发货通知单{listusers.ProjectName}{DeliveryNoticeLou}" + _operator.UserId + "已完成发货通知审批";
                            }
                            else
                            {
                                result.Str = $"发货通知单{listusers.ProjectName}{DeliveryNoticeLou}" + "待审批" + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]";
                            }
                            string btn = _objMobs["Action"]["Name"].ToString();
                            result.BillId = input.BillId;


                            //查询出发货通知单申请人
                            if (listusers != null)
                            {
                                var UserDing = await Db.GetIQueryable<Base_User>().Where(m => m.Id == listusers.CreatorId).FirstOrDefaultAsync();
                                applicantList.Add(UserDing.DingUserId);
                            }
                            result.ExecutorIds = UserList;
                            result.MessgUrl = "eapp://pages/info/info?billid=" + input.BillId + "&title=" + _objMobs["StartState"]["Name"].ToString() + "&btn=" + btn + "&id=" + input.Id + "";
                            DingHelper.sendMessage(result);
                            if (farr.Count() == curlevel + 1)
                            {
                                Applicantresuit.Str = $"发货通知:{listusers.ProjectName}{DeliveryNoticeLou}" + baseuser.RealName + "已审批" + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]";
                            }
                            else
                            {
                                Applicantresuit.Str = $"发货通知:{listusers.ProjectName}{DeliveryNoticeLou}" + baseuser.RealName + "已审批" + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]";
                            }
                            Applicantresuit.BillId = input.BillId;
                            Applicantresuit.ExecutorIds = applicantList;
                            Applicantresuit.MessgUrl = $"eapp://pages/infoView/infoView?BillId={input.BillId}&OverFlowId={input.Id}";
                            DingHelper.sendMessage(Applicantresuit);


                            break;
                        #region 进场通知审批流
                        case "MobilizationNotice"://进场通知
                            List<string> applicantLists = new List<string>();
                            List<string> ulist = new List<string>();
                            var listuserss = await Db.GetIQueryable<PB_MobilizationNotice>().Where(m => m.Id == input.BillId).FirstOrDefaultAsync();
                            //获取进场通知单数据
                            StringBuilder sql = new StringBuilder();
                            sql.Append("SELECT ");
                            sql.Append("b.Content,");
                            sql.Append("ifnull(c.LEVEL,0) LEVEL ");
                            sql.Append("FROM ");
                            sql.Append("PB_MobilizationNotice a ");
                            sql.Append("LEFT JOIN Base_OverFlow b ON a.WorkFlow = b.Id ");
                            sql.Append("LEFT JOIN Base_OverFlowTask c ON a.Id = c.BillId  ");
                            sql.Append("WHERE ");
                            sql.Append("a.Id = '" + input.BillId + "'");
                            //推送创建进场通知单用户钉钉信息
                            string uidsss = "";
                            var flowMobss = await Db.GetListBySqlAsync<CurrentOverFlowDTO>(sql.ToString());
                            if (flowMobss.IsNullOrEmpty())
                            {
                                throw new BusException("当前单据已审批完成!");
                            }
                            JArray farrMobss = JArray.Parse(flowMobss[0].Content);

                            JObject _objMobss = JObject.Parse(farrMobss[flowMobss[0].Level].ToString());
                            //推送下一级人员id
                            JArray usrMobss = JArray.Parse(_objMobss["Assignees"]["UserId"].ToString());
                            JToken[] liststrs = _objMobss["Assignees"]["UserId"].ToArray();
                            if (liststrs.Length > 0)
                            {
                                foreach (var udingid in liststrs)
                                {
                                    string userid = udingid.ToString();
                                    var base_user = await Db.GetIQueryable<Base_User>().Where(m => m.Id == userid).FirstOrDefaultAsync();
                                    if (base_user != null)
                                    {
                                        ulist.Add(base_user.DingUserId);
                                    }
                                }
                            }
                            else
                            {
                                string rid = _objMobss["Assignees"]["RoleId"].ToString();
                                var emps = await Db.GetListBySqlAsync<Base_User>("SELECT * FROM Base_User WHERE UserPost = (SELECT RoleName FROM Base_Role WHERE Id='" + rid + "')");
                                foreach (var e in emps)
                                {
                                    ulist.Add(e.DingUserId);
                                }
                            }
                            var Base_OverFlowLogTop1s = await Db.GetIQueryable<Base_OverFlowLog>().Where(m => m.BillId == input.BillId).OrderByDescending(m => m.CreateTime).FirstOrDefaultAsync();
                            var baseusers = await Db.GetIQueryable<Base_User>().Where(m => m.Id == Base_OverFlowLogTop1s.CreatorId).FirstOrDefaultAsync();
                            var item = Db.GetIQueryable<PB_MobilzationItem>().Where(m => m.ParentId == input.BillId).ToList();
                            string PB_MobilLou = "";
                            foreach (var i in item)
                            {
                                PB_MobilLou += i.BuildingNo;
                            }
                            if (farr.Count() == curlevel + 1)
                            {
                                result.Str = $"进场通知: {listuserss.ProjectName}{PB_MobilLou}" + _operator.UserName + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]已完成进场通知审批";
                            }
                            else
                            {
                                result.Str = $"进场通知: {listuserss.ProjectName}{PB_MobilLou}" + _operator.UserName + "待审批" + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]";
                            }
                            string btns = _objMobss["Action"]["Name"].ToString();
                            result.BillId = input.BillId;

                            if (listuserss != null)
                            {
                                var UserDing = await Db.GetIQueryable<Base_User>().Where(m => m.Id == listuserss.CreatorId).FirstOrDefaultAsync();
                                applicantLists.Add(UserDing.DingUserId);
                            }
                            result.ExecutorIds = ulist;
                            result.MessgUrl = "eapp://pages/info/info?billid=" + input.BillId + "&title=" + _objMobss["StartState"]["Name"].ToString() + "&btn=" + btns + "&id=" + input.Id + "";
                            DingHelper.sendMessage(result);
                            if (farr.Count() == curlevel + 1)
                            {
                                Applicantresuit.Str = $"进场通知: {listuserss.ProjectName} {PB_MobilLou}" + baseusers.RealName + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]已完成进场通知审批";
                            }
                            else
                            {
                                Applicantresuit.Str = $"进场通知:{listuserss.ProjectName}{PB_MobilLou}" + baseusers.RealName + "已审批" + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]";
                            }
                            Applicantresuit.BillId = input.BillId;
                            Applicantresuit.ExecutorIds = applicantLists;
                            Applicantresuit.MessgUrl = $"eapp://pages/infoView/infoView?BillId={input.BillId}&OverFlowId={input.Id}";
                            DingHelper.sendMessage(Applicantresuit);
                            break;
                        #endregion
                        #region 回货通知审批流
                        case "ReturnNotice"://回货通知
                            List<string> UserListreturn = new List<string>();
                            List<string> applicantListreturn = new List<string>();
                            StringBuilder DeliveryNoticeSqlreturn = new StringBuilder();
                            DeliveryNoticeSqlreturn.Append("Select ");
                            DeliveryNoticeSqlreturn.Append("b.Content,");
                            DeliveryNoticeSqlreturn.Append("ifnull(c.LEVEL,0) LEVEL ");
                            DeliveryNoticeSqlreturn.Append("FROM ");
                            DeliveryNoticeSqlreturn.Append("PB_DeliveryNotice a ");
                            DeliveryNoticeSqlreturn.Append("LEFT JOIN Base_OverFlow b ON a.WorkFlow = b.Id  ");
                            DeliveryNoticeSqlreturn.Append("LEFT JOIN Base_OverFlowTask c ON a.Id = c.BillId ");
                            DeliveryNoticeSqlreturn.Append("WHERE ");
                            DeliveryNoticeSqlreturn.Append("a.Id='" + input.BillId + "'");
                            string uidssreturn = "";
                            var flowMobsreturn = await Db.GetListBySqlAsync<CurrentOverFlowDTO>(DeliveryNoticeSqlreturn.ToString());
                            if (flowMobsreturn.IsNullOrEmpty())
                            {
                                throw new BusException("当前单据已审批完成!");
                            }
                            JArray farrMobsreturn = JArray.Parse(flowMobsreturn[0].Content);
                            JObject _objMobsreturn = JObject.Parse(farrMobsreturn[flowMobsreturn[0].Level].ToString());
                            JArray usrMobsreturn = JArray.Parse(_objMobsreturn["Assignees"]["UserId"].ToString());
                            JToken[] liststrreturn = _objMobsreturn["Assignees"]["UserId"].ToArray();
                            if (liststrreturn.Length > 0)
                            {
                                foreach (var udingid in liststrreturn)
                                {
                                    string userid = udingid.ToString();
                                    var base_user = await Db.GetIQueryable<Base_User>().Where(m => m.Id == userid).FirstOrDefaultAsync();
                                    if (base_user != null)
                                    {
                                        UserListreturn.Add(base_user.DingUserId);
                                    }
                                }
                            }
                            else
                            {
                                string rid = _objMobsreturn["Assignees"]["RoleId"].ToString();
                                var emps = await Db.GetListBySqlAsync<Base_User>("SELECT * FROM Base_User WHERE UserPost = (SELECT RoleName FROM Base_Role WHERE Id='" + rid + "')");
                                foreach (var e in emps)
                                {
                                    UserListreturn.Add(e.DingUserId);
                                }

                            }
                            var listusersreturn = await Db.GetIQueryable<PB_DeliveryNotice>().Where(m => m.Id == input.BillId).FirstOrDefaultAsync();
                            var Base_OverFlowLogTop1return = await Db.GetIQueryable<Base_OverFlowLog>().Where(m => m.BillId == input.BillId).OrderByDescending(m => m.CreateTime).FirstOrDefaultAsync();
                            var baseuserreturn = await Db.GetIQueryable<Base_User>().Where(m => m.Id == Base_OverFlowLogTop1return.CreatorId).FirstOrDefaultAsync();
                            if (farr.Count() == curlevel + 1)
                            {
                                result.Str = "回货通知:" + baseuserreturn.RealName + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]已完成回货通知审批";
                            }
                            else
                            {
                                result.Str = "回货通知:" + listusersreturn.ProjectName + _operator.UserName + "待审批" + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]";
                            }
                            string btnreturn = _objMobsreturn["Action"]["Name"].ToString();
                            result.BillId = input.BillId;
                            if (listusersreturn != null)
                            {
                                var UserDing = await Db.GetIQueryable<Base_User>().Where(m => m.Id == listusersreturn.CreatorId).FirstOrDefaultAsync();
                                applicantListreturn.Add(UserDing.DingUserId);
                            }
                            result.ExecutorIds = UserListreturn;
                            result.MessgUrl = "eapp://pages/info/info?billid=" + input.BillId + "&title=" + _objMobsreturn["StartState"]["Name"].ToString() + "&btn=" + btnreturn + "&id=" + input.Id + "";
                            DingHelper.sendMessage(result);
                            if (farr.Count() == curlevel + 1)
                            {
                                Applicantresuit.Str = "回货通知" + baseuserreturn.RealName + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]已完成回货通知审批";
                            }
                            else
                            {
                                Applicantresuit.Str = $"回货通知:{listusersreturn.ProjectName}" + baseuserreturn.RealName + "已审批" + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]";
                            }
                            Applicantresuit.BillId = input.BillId;
                            Applicantresuit.ExecutorIds = applicantListreturn;
                            Applicantresuit.MessgUrl = $"eapp://pages/infoView/infoView?BillId={input.BillId}";
                            DingHelper.sendMessage(Applicantresuit);
                            break;
                        case "DesignList":
                            var isinsert = await Db.GetIQueryable<PB_DesignListFile>().Where(m => m.DesignId == input.BillId && m.Type == 0).ToListAsync();
                            if (isinsert.Count == 0)
                            {
                                PB_DesignListFile pB_DesignListFile = new PB_DesignListFile();
                                pB_DesignListFile.Id = IdHelper.GetId();
                                pB_DesignListFile.CreatorId = _operator.UserId;
                                pB_DesignListFile.CreateTime = DateTime.Now;
                                pB_DesignListFile.DetailedFile = "";
                                pB_DesignListFile.DesignId = input.BillId;
                                pB_DesignListFile.VersionNumber = 0;
                                pB_DesignListFile.Type = 0;
                                pB_DesignListFile.Deleted = false;
                                await Db.InsertAsync(pB_DesignListFile);
                            }



                            var PB_DesignListModel = await Db.GetIQueryable<PB_DesignList>().Where(m => m.Id == input.BillId).FirstOrDefaultAsync();
                            var ProjectModel = await Db.GetIQueryable<PB_Project>().Where(m => m.Id == PB_DesignListModel.ProjectId).FirstOrDefaultAsync();
                            var BuildingnumberModel = await Db.GetIQueryable<PB_Buildingnumber>().Where(m => m.Id == PB_DesignListModel.BuildingNumberID).FirstOrDefaultAsync();
                            List<string> ApproverList = new List<string>();//向审批人推送
                            List<string> ApplicantList = new List<string>();//向申请人推送
                            List<string> AppcclicantList = new List<string>();//向抄送人推送
                            StringBuilder DesignListSql = new StringBuilder();
                            DesignListSql.AppendLine("Select ");
                            DesignListSql.AppendLine("Base_OverFlow.Content,");
                            DesignListSql.AppendLine("ifnull( Base_OverFlowTask.LEVEL, 0 ) LEVEL  ");
                            DesignListSql.AppendLine("FROM ");
                            DesignListSql.AppendLine("PB_DesignList");
                            DesignListSql.AppendLine("INNER JOIN Base_OverFlowTask ON Base_OverFlowTask.BillId = PB_DesignList.Id");
                            DesignListSql.AppendLine("INNER JOIN Base_OverFlow ON Base_OverFlow.BillType = Base_OverFlowTask.BillType ");
                            DesignListSql.AppendLine("WHERE ");
                            DesignListSql.AppendLine("PB_DesignList.Id = '" + input.BillId + "'");
                            string uidssreturnDesignList = "";
                            var flowDesignList = await Db.GetListBySqlAsync<CurrentOverFlowDTO>(DesignListSql.ToString());
                            if (flowDesignList.IsNullOrEmpty())
                            {
                                throw new BusException("当前单据已审批完成!");
                            }
                            JArray farrDesignList = JArray.Parse(flowDesignList[0].Content);
                            JObject _objDesignList = JObject.Parse(farrDesignList[flowDesignList[0].Level].ToString());
                            JArray usrMobDesignList = JArray.Parse(_objDesignList["Assignees"]["UserId"].ToString());
                            JToken[] ApproverLists = _objDesignList["Assignees"]["UserId"].ToArray();
                            if (ApproverLists.Length > 0)
                            {
                                foreach (var udingid in ApproverLists)
                                {
                                    string userid = udingid.ToString();
                                    var base_user = await Db.GetIQueryable<Base_User>().Where(m => m.Id == userid).FirstOrDefaultAsync();
                                    if (base_user != null)
                                    {
                                        ApproverList.Add(base_user.DingUserId);
                                    }
                                }
                            }
                            else
                            {
                                string rid = _objDesignList["Assignees"]["RoleId"].ToString();
                                var emps = await Db.GetListBySqlAsync<Base_User>("SELECT * FROM Base_User WHERE UserPost = (SELECT RoleName FROM Base_Role WHERE Id='" + rid + "')");
                                foreach (var e in emps)
                                {
                                    ApproverList.Add(e.DingUserId);
                                }

                            }
                            var listuserDesignList = await Db.GetIQueryable<PB_DesignList>().Where(m => m.Id == input.BillId).FirstOrDefaultAsync();
                            var Base_OverFlowLogDesignList = await Db.GetIQueryable<Base_OverFlowLog>().Where(m => m.BillId == input.BillId).OrderByDescending(m => m.CreateTime).FirstOrDefaultAsync();
                            var baseuserDesignList = await Db.GetIQueryable<Base_User>().Where(m => m.Id == Base_OverFlowLogDesignList.CreatorId).FirstOrDefaultAsync();
                            if (farr.Count() == curlevel + 1)
                            {
                                result.Str = $"设计清单:{ProjectModel.Name}{BuildingnumberModel.Buildingnumber} " + baseuserDesignList.RealName + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]已设计清单审批";
                            }
                            else
                            {
                                result.Str = $"设计清单:{ProjectModel.Name}{BuildingnumberModel.Buildingnumber}" + _operator.UserName + "待审批" + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]";
                            }
                            string btnreturnDesignList = _objDesignList["Action"]["Name"].ToString();
                            result.BillId = input.BillId;
                            if (listuserDesignList != null)
                            {
                                var UserDing = await Db.GetIQueryable<Base_User>().Where(m => m.Id == listuserDesignList.CreatorId).FirstOrDefaultAsync();
                                ApplicantList.Add(UserDing.DingUserId);
                            }
                            result.ExecutorIds = ApproverList;
                            result.MessgUrl = "eapp://pages/info/info?billid=" + input.BillId + "&title=" + _objDesignList["StartState"]["Name"].ToString() + "&btn=" + btnreturnDesignList + "&id=" + input.Id + "";
                            DingHelper.sendMessage(result);
                            if (farr.Count() == curlevel + 1)
                            {
                                Applicantresuit.Str = $"设计清单:{ProjectModel.Name}{BuildingnumberModel.Buildingnumber}" + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]已完成设计清单审批";
                            }
                            else
                            {
                                Applicantresuit.Str = $"设计清单:{ProjectModel.Name}{BuildingnumberModel.Buildingnumber}" + baseuserDesignList.RealName + "已审批" + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]";
                            }
                            Applicantresuit.BillId = input.BillId;
                            Applicantresuit.ExecutorIds = ApplicantList;
                            Applicantresuit.MessgUrl = $"eapp://pages/infoView/infoView?BillId={input.BillId}&OverFlowId={input.Id}&Type=0";
                            DingHelper.sendMessage(Applicantresuit);
                            //抄送人列表
                            JToken[] ApprovCCLists = _objDesignList["Assignees"]["PersonId"].ToArray();
                            if (ApprovCCLists.Length > 0)
                            {
                                foreach (var udingid in ApprovCCLists)
                                {
                                    string userid = udingid.ToString();
                                    var base_user = await Db.GetIQueryable<Base_User>().Where(m => m.Id == userid).FirstOrDefaultAsync();
                                    if (base_user != null)
                                    {
                                        AppcclicantList.Add(base_user.DingUserId);
                                    }
                                }
                            }
                            else
                            {
                                string rid = _objDesignList["Assignees"]["RoleId"].ToString();
                                var emps = await Db.GetListBySqlAsync<Base_User>("SELECT * FROM Base_User WHERE UserPost = (SELECT RoleName FROM Base_Role WHERE Id='" + rid + "')");
                                foreach (var e in emps)
                                {
                                    AppcclicantList.Add(e.DingUserId);
                                }
                            }
                            CClist.Str = $"设计清单:{ProjectModel.Name}{BuildingnumberModel.Buildingnumber}{baseuserDesignList.RealName }已审批" + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]";
                            CClist.BillId = input.BillId;
                            CClist.ExecutorIds = AppcclicantList;
                            CClist.MessgUrl = $"eapp://pages/infoView/infoView?BillId={input.BillId}&OverFlowId={input.Id}&Type=0";
                            DingHelper.sendMessage(CClist);

                            break;
                        #endregion
                        #region 设计清单补加单钉钉审批
                        case "InsertDesignList":
                            var isinsert1 = await Db.GetIQueryable<PB_DesignListFile>().Where(m => m.DesignId == input.BillId && m.Type == 1).ToListAsync();
                            if (isinsert1.Count == 0)
                            {
                                PB_DesignListFile pB_DesignListFile = new PB_DesignListFile();
                                pB_DesignListFile.Id = IdHelper.GetId();
                                pB_DesignListFile.CreatorId = _operator.UserId;
                                pB_DesignListFile.CreateTime = DateTime.Now;
                                pB_DesignListFile.DetailedFile = "";
                                pB_DesignListFile.DesignId = input.BillId;
                                pB_DesignListFile.VersionNumber = 1;
                                pB_DesignListFile.Type = 1;
                                pB_DesignListFile.Deleted = false;
                                await Db.InsertAsync(pB_DesignListFile);
                            }
                            var PB_DesignListModelInsert = await Db.GetIQueryable<PB_DesignList>().Where(m => m.Id == input.BillId).FirstOrDefaultAsync();
                            var ProjectModelInsert = await Db.GetIQueryable<PB_Project>().Where(m => m.Id == PB_DesignListModelInsert.ProjectId).FirstOrDefaultAsync();
                            var BuildingnumberModelInsert = await Db.GetIQueryable<PB_Buildingnumber>().Where(m => m.Id == PB_DesignListModelInsert.BuildingNumberID).FirstOrDefaultAsync();
                            List<string> ApproverListInsert = new List<string>();//向审批人推送
                            List<string> ApplicantListInsert = new List<string>();//向申请人推送
                            List<string> AppcclicantListInsert = new List<string>();//向抄送人推送
                            StringBuilder DesignListSqlInsert = new StringBuilder();
                            DesignListSqlInsert.AppendLine("Select ");
                            DesignListSqlInsert.AppendLine("Base_OverFlow.Content,");
                            DesignListSqlInsert.AppendLine("ifnull( Base_OverFlowTask.LEVEL, 0 ) LEVEL  ");
                            DesignListSqlInsert.AppendLine("FROM ");
                            DesignListSqlInsert.AppendLine("PB_DesignList");
                            DesignListSqlInsert.AppendLine("INNER JOIN Base_OverFlowTask ON Base_OverFlowTask.BillId = PB_DesignList.Id");
                            DesignListSqlInsert.AppendLine("INNER JOIN Base_OverFlow ON Base_OverFlow.BillType = Base_OverFlowTask.BillType ");
                            DesignListSqlInsert.AppendLine("WHERE ");
                            DesignListSqlInsert.AppendLine("PB_DesignList.Id = '" + input.BillId + "'");
                            string uidssreturnDesignListInsert = "";
                            var flowDesignListInsert = await Db.GetListBySqlAsync<CurrentOverFlowDTO>(DesignListSqlInsert.ToString());
                            if (flowDesignListInsert.IsNullOrEmpty())
                            {
                                throw new BusException("当前单据已审批完成!");
                            }
                            JArray farrDesignListInsert = JArray.Parse(flowDesignListInsert[0].Content);
                            JObject _objDesignListInsert = JObject.Parse(farrDesignListInsert[flowDesignListInsert[0].Level].ToString());
                            JArray usrMobDesignListInsert = JArray.Parse(_objDesignListInsert["Assignees"]["UserId"].ToString());
                            JToken[] ApproverListsInsert = _objDesignListInsert["Assignees"]["UserId"].ToArray();
                            if (ApproverListsInsert.Length > 0)
                            {
                                foreach (var udingid in ApproverListsInsert)
                                {
                                    string userid = udingid.ToString();
                                    var base_user = await Db.GetIQueryable<Base_User>().Where(m => m.Id == userid).FirstOrDefaultAsync();
                                    if (base_user != null)
                                    {
                                        ApproverListInsert.Add(base_user.DingUserId);
                                    }
                                }
                            }
                            else
                            {
                                string rid = _objDesignListInsert["Assignees"]["RoleId"].ToString();
                                var emps = await Db.GetListBySqlAsync<Base_User>("SELECT * FROM Base_User WHERE UserPost = (SELECT RoleName FROM Base_Role WHERE Id='" + rid + "')");
                                foreach (var e in emps)
                                {
                                    ApproverListInsert.Add(e.DingUserId);
                                }

                            }
                            var listuserDesignListInsert = await Db.GetIQueryable<PB_DesignList>().Where(m => m.Id == input.BillId).FirstOrDefaultAsync();
                            var Base_OverFlowLogDesignListInsert = await Db.GetIQueryable<Base_OverFlowLog>().Where(m => m.BillId == input.BillId).OrderByDescending(m => m.CreateTime).FirstOrDefaultAsync();
                            var baseuserDesignListInsert = await Db.GetIQueryable<Base_User>().Where(m => m.Id == Base_OverFlowLogDesignListInsert.CreatorId).FirstOrDefaultAsync();
                            if (farr.Count() == curlevel + 1)
                            {
                                result.Str = $"设计清单补加单:{ProjectModelInsert.Name}{BuildingnumberModelInsert.Buildingnumber} " + baseuserDesignListInsert.RealName + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]已设计清单审批";
                            }
                            else
                            {
                                result.Str = $"设计清单补加单:{ProjectModelInsert.Name}{BuildingnumberModelInsert.Buildingnumber}" + _operator.UserName + "待审批" + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]";
                            }
                            string btnreturnDesignListInsert = _objDesignListInsert["Action"]["Name"].ToString();
                            result.BillId = input.BillId;
                            if (listuserDesignListInsert != null)
                            {
                                var UserDing = await Db.GetIQueryable<Base_User>().Where(m => m.Id == listuserDesignListInsert.CreatorId).FirstOrDefaultAsync();
                                ApplicantListInsert.Add(UserDing.DingUserId);
                            }
                            result.ExecutorIds = ApproverListInsert;
                            result.MessgUrl = "eapp://pages/info/info?billid=" + input.BillId + "&title=" + _objDesignListInsert["StartState"]["Name"].ToString() + "&btn=" + btnreturnDesignListInsert + "&id=" + input.Id + "";
                            DingHelper.sendMessage(result);
                            if (farr.Count() == curlevel + 1)
                            {
                                Applicantresuit.Str = $"设计清单补加单:{ProjectModelInsert.Name}{BuildingnumberModelInsert.Buildingnumber}" + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]已完成设计清单审批";
                            }
                            else
                            {
                                Applicantresuit.Str = $"设计清单补给单:{ProjectModelInsert.Name}{BuildingnumberModelInsert.Buildingnumber}" + baseuserDesignListInsert.RealName + "已审批" + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]";
                            }
                            Applicantresuit.BillId = input.BillId;
                            Applicantresuit.ExecutorIds = ApplicantListInsert;
                            Applicantresuit.MessgUrl = $"eapp://pages/infoView/infoView?BillId={input.BillId}&OverFlowId={input.Id}&Type=1";
                            DingHelper.sendMessage(Applicantresuit);


                            //抄送人列表
                            JToken[] ApprovCCListsInsert = _objDesignListInsert["Assignees"]["PersonId"].ToArray();
                            if (ApprovCCListsInsert.Length > 0)
                            {
                                foreach (var udingid in ApprovCCListsInsert)
                                {
                                    string userid = udingid.ToString();
                                    var base_user = await Db.GetIQueryable<Base_User>().Where(m => m.Id == userid).FirstOrDefaultAsync();
                                    if (base_user != null)
                                    {
                                        AppcclicantListInsert.Add(base_user.DingUserId);
                                    }
                                }
                            }
                            else
                            {
                                string rid = _objDesignListInsert["Assignees"]["RoleId"].ToString();
                                var emps = await Db.GetListBySqlAsync<Base_User>("SELECT * FROM Base_User WHERE UserPost = (SELECT RoleName FROM Base_Role WHERE Id='" + rid + "')");
                                foreach (var e in emps)
                                {
                                    AppcclicantListInsert.Add(e.DingUserId);
                                }
                            }
                            CClist.Str = $"设计清单补加单:{ProjectModelInsert.Name}{BuildingnumberModelInsert.Buildingnumber}{baseuserDesignListInsert.RealName }已审批" + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]";
                            CClist.BillId = input.BillId;
                            CClist.ExecutorIds = AppcclicantListInsert;
                            CClist.MessgUrl = $"eapp://pages/infoView/infoView?BillId={input.BillId}&OverFlowId={input.Id}&Type=1";
                            DingHelper.sendMessage(CClist);

                            break;
                        #endregion
                        #region 设计清单变更单审批
                        case "ChangeDesignList"://设计清单变更单钉钉消息推送
                            var isinsert2 = await Db.GetIQueryable<PB_DesignListFile>().Where(m => m.DesignId == input.BillId && m.Type == 2).ToListAsync();
                            if (isinsert2.Count == 0)
                            {
                                PB_DesignListFile pB_DesignListFile = new PB_DesignListFile();
                                pB_DesignListFile.Id = IdHelper.GetId();
                                pB_DesignListFile.CreatorId = _operator.UserId;
                                pB_DesignListFile.CreateTime = DateTime.Now;
                                pB_DesignListFile.DetailedFile = "";
                                pB_DesignListFile.DesignId = input.BillId;
                                pB_DesignListFile.VersionNumber = 1;
                                pB_DesignListFile.Type = 2;
                                pB_DesignListFile.Deleted = false;
                                await Db.InsertAsync(pB_DesignListFile);
                            }
                            var PB_DesignListModelChange = await Db.GetIQueryable<PB_DesignList>().Where(m => m.Id == input.BillId).FirstOrDefaultAsync();
                            var ProjectModelChange = await Db.GetIQueryable<PB_Project>().Where(m => m.Id == PB_DesignListModelChange.ProjectId).FirstOrDefaultAsync();
                            var BuildingnumberModelChange = await Db.GetIQueryable<PB_Buildingnumber>().Where(m => m.Id == PB_DesignListModelChange.BuildingNumberID).FirstOrDefaultAsync();
                            List<string> ApproverListChange = new List<string>();//向审批人推送
                            List<string> ApplicantListChange = new List<string>();//向申请人推送
                            List<string> AppcclicantListChange = new List<string>();//向抄送人推送
                            StringBuilder DesignListSqlChange = new StringBuilder();
                            DesignListSqlChange.AppendLine("Select ");
                            DesignListSqlChange.AppendLine("Base_OverFlow.Content,");
                            DesignListSqlChange.AppendLine("ifnull( Base_OverFlowTask.LEVEL, 0 ) LEVEL  ");
                            DesignListSqlChange.AppendLine("FROM ");
                            DesignListSqlChange.AppendLine("PB_DesignList");
                            DesignListSqlChange.AppendLine("INNER JOIN Base_OverFlowTask ON Base_OverFlowTask.BillId = PB_DesignList.Id");
                            DesignListSqlChange.AppendLine("INNER JOIN Base_OverFlow ON Base_OverFlow.BillType = Base_OverFlowTask.BillType ");
                            DesignListSqlChange.AppendLine("WHERE ");
                            DesignListSqlChange.AppendLine("PB_DesignList.Id = '" + input.BillId + "'");
                            string uidssreturnDesignListChange = "";
                            var flowDesignListChange = await Db.GetListBySqlAsync<CurrentOverFlowDTO>(DesignListSqlChange.ToString());
                            JArray farrDesignListChange = JArray.Parse(flowDesignListChange[0].Content);
                            JObject _objDesignListChange = JObject.Parse(farrDesignListChange[flowDesignListChange[0].Level].ToString());
                            JArray usrMobDesignListChange = JArray.Parse(_objDesignListChange["Assignees"]["UserId"].ToString());
                            JToken[] ApproverListsChange = _objDesignListChange["Assignees"]["UserId"].ToArray();
                            if (ApproverListsChange.Length > 0)
                            {
                                foreach (var udingid in ApproverListsChange)
                                {
                                    string userid = udingid.ToString();
                                    var base_user = await Db.GetIQueryable<Base_User>().Where(m => m.Id == userid).FirstOrDefaultAsync();
                                    if (base_user != null)
                                    {
                                        ApproverListChange.Add(base_user.DingUserId);
                                    }
                                }
                            }
                            else
                            {
                                string rid = _objDesignListChange["Assignees"]["RoleId"].ToString();
                                var emps = await Db.GetListBySqlAsync<Base_User>("SELECT * FROM Base_User WHERE UserPost = (SELECT RoleName FROM Base_Role WHERE Id='" + rid + "')");
                                foreach (var e in emps)
                                {
                                    ApproverListChange.Add(e.DingUserId);
                                }

                            }
                            var listuserDesignListChange = await Db.GetIQueryable<PB_DesignList>().Where(m => m.Id == input.BillId).FirstOrDefaultAsync();
                            var Base_OverFlowLogDesignListChange = await Db.GetIQueryable<Base_OverFlowLog>().Where(m => m.BillId == input.BillId).OrderByDescending(m => m.CreateTime).FirstOrDefaultAsync();
                            var baseuserDesignListChange = await Db.GetIQueryable<Base_User>().Where(m => m.Id == Base_OverFlowLogDesignListChange.CreatorId).FirstOrDefaultAsync();
                            if (farr.Count() == curlevel + 1)
                            {
                                result.Str = $"设计清单变更单:{ProjectModelChange.Name}{BuildingnumberModelChange.Buildingnumber} " + baseuserDesignListChange.RealName + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]已设计清单审批";
                            }
                            else
                            {
                                result.Str = $"设计清单变更单:{ProjectModelChange.Name}{BuildingnumberModelChange.Buildingnumber}" + _operator.UserName + "待审批" + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]";
                            }
                            string btnreturnDesignListChange = _objDesignListChange["Action"]["Name"].ToString();
                            result.BillId = input.BillId;
                            if (listuserDesignListChange != null)
                            {
                                var UserDing = await Db.GetIQueryable<Base_User>().Where(m => m.Id == listuserDesignListChange.CreatorId).FirstOrDefaultAsync();
                                ApplicantListChange.Add(UserDing.DingUserId);
                            }
                            result.ExecutorIds = ApproverListChange;
                            result.MessgUrl = "eapp://pages/info/info?billid=" + input.BillId + "&title=" + _objDesignListChange["StartState"]["Name"].ToString() + "&btn=" + btnreturnDesignListChange + "&id=" + input.Id + "";
                            DingHelper.sendMessage(result);
                            if (farr.Count() == curlevel + 1)
                            {
                                Applicantresuit.Str = $"设计清单变更单:{ProjectModelChange.Name}{BuildingnumberModelChange.Buildingnumber}" + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]已完成设计清单审批";
                            }
                            else
                            {
                                Applicantresuit.Str = $"设计清单变更单:{ProjectModelChange.Name}{BuildingnumberModelChange.Buildingnumber}" + baseuserDesignListChange.RealName + "已审批" + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]";
                            }
                            Applicantresuit.BillId = input.BillId;
                            Applicantresuit.ExecutorIds = ApplicantListChange;
                            Applicantresuit.MessgUrl = $"eapp://pages/infoView/infoView?BillId={input.BillId}&OverFlowId={input.Id}&Type=2";
                            DingHelper.sendMessage(Applicantresuit);
                            //抄送人列表
                            JToken[] ApprovCCListsChange = _objDesignListChange["Assignees"]["PersonId"].ToArray();
                            if (ApprovCCListsChange.Length > 0)
                            {
                                foreach (var udingid in ApprovCCListsChange)
                                {
                                    string userid = udingid.ToString();
                                    var base_user = await Db.GetIQueryable<Base_User>().Where(m => m.Id == userid).FirstOrDefaultAsync();
                                    if (base_user != null)
                                    {
                                        AppcclicantListChange.Add(base_user.DingUserId);
                                    }
                                }
                            }
                            else
                            {
                                string rid = _objDesignListChange["Assignees"]["RoleId"].ToString();
                                var emps = await Db.GetListBySqlAsync<Base_User>("SELECT * FROM Base_User WHERE UserPost = (SELECT RoleName FROM Base_Role WHERE Id='" + rid + "')");
                                foreach (var e in emps)
                                {
                                    AppcclicantListChange.Add(e.DingUserId);
                                }
                            }
                            CClist.Str = $"设计清单变更单:{ProjectModelChange.Name}{BuildingnumberModelChange.Buildingnumber}{baseuserDesignListChange.RealName }已审批" + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]";
                            CClist.BillId = input.BillId;
                            CClist.ExecutorIds = AppcclicantListChange;
                            CClist.MessgUrl = $"eapp://pages/infoView/infoView?BillId={input.BillId}&OverFlowId={input.Id}&Type=2";
                            DingHelper.sendMessage(CClist);
                            break;
                        #endregion
                        default:
                            break;
                    }

                }
                else
                {
                    //利用if条件判断传入的build是哪种类型

                    //进入发货通知操作
                    var aa = await Db.GetIQueryable<PB_DeliveryNotice>().Where(m => m.Id == input.BillId).FirstOrDefaultAsync();
                    if (aa != null)
                    {
                        List<string> applicantList = new List<string>();
                        var Base_OverFlowLogTop1 = await Db.GetIQueryable<Base_OverFlowLog>().Where(m => m.BillId == input.BillId).OrderByDescending(m => m.CreateTime).FirstOrDefaultAsync();
                        var baseuser = await Db.GetIQueryable<Base_User>().Where(m => m.Id == Base_OverFlowLogTop1.CreatorId).FirstOrDefaultAsync();
                        List<string> UserList = new List<string>();
                        Util.Helper.DingInputDTO result = new Util.Helper.DingInputDTO();
                        var listusers = await Db.GetIQueryable<PB_DeliveryNotice>().Where(m => m.Id == input.BillId).FirstOrDefaultAsync();
                        if (listusers != null)
                        {
                            var UserDing = await Db.GetIQueryable<Base_User>().Where(m => m.Id == listusers.CreatorId).FirstOrDefaultAsync();
                            applicantList.Add(UserDing.DingUserId);
                        }
                        var logs = await Db.GetIQueryable<Base_OverFlowLog>().OrderByDescending(m => m.CreateTime).Where(m => m.BillId == aa.Id).FirstOrDefaultAsync();
                        var user = await Db.GetIQueryable<Base_User>().Where(m => m.Id == logs.CreatorId).FirstOrDefaultAsync();
                        if (aa.Type == 1)
                        {
                            var item = Db.GetIQueryable<PB_DeliveryNoticeItem>().Where(m => m.ParentId == aa.Id).ToList();
                            string Buing = "";
                            foreach (var buing in item)
                            {
                                Buing += buing.BuildingNo;
                            }

                            result.Str = "发货通知:" + listusers.ProjectName + Buing + user.RealName + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]" + "已完成"; ;
                            result.ExecutorIds = applicantList;
                            result.MessgUrl = $"eapp://pages/infoView/infoView?BillId={input.BillId}&OverFlowId={input.Id}";
                            DingHelper.sendMessage(result);
                            string sqlupdateApprovalTime = "update PB_DeliveryNotice set ApprovalTime='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "'  where Id='" + input.BillId + "'";
                            await Db.ExecuteSqlAsync(sqlupdateApprovalTime);
                        }
                        else
                        {
                            result.Str = "回货通知" + listusers.ProjectName + user.RealName + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]" + " 已完成"; ;
                            result.ExecutorIds = applicantList;
                            result.MessgUrl = $"eapp://pages/infoView/infoView?BillId={input.BillId}&OverFlowId={input.Id}";
                            DingHelper.sendMessage(result);
                        }

                    }

                    //设计清单操作
                    var cc = await Db.GetIQueryable<PB_DesignList>().Where(m => m.Id == input.BillId).FirstOrDefaultAsync();
                    if (cc != null)
                    {
                        //钉钉完成推送申请人
                        var PB_DesignListModel = await Db.GetIQueryable<PB_DesignList>().Where(m => m.Id == input.BillId).FirstOrDefaultAsync();
                        var ProjectModel = await Db.GetIQueryable<PB_Project>().Where(m => m.Id == PB_DesignListModel.ProjectId).FirstOrDefaultAsync();
                        var BuildingnumberModel = await Db.GetIQueryable<PB_Buildingnumber>().Where(m => m.Id == PB_DesignListModel.BuildingNumberID).FirstOrDefaultAsync();
                        List<string> applicantList = new List<string>();
                        var Base_OverFlowLogTop1 = await Db.GetIQueryable<Base_OverFlowLog>().Where(m => m.BillId == input.BillId).OrderByDescending(m => m.CreateTime).FirstOrDefaultAsync();
                        var baseuser = await Db.GetIQueryable<Base_User>().Where(m => m.Id == Base_OverFlowLogTop1.CreatorId).FirstOrDefaultAsync();
                        List<string> UserList = new List<string>();
                        Util.Helper.DingInputDTO result = new Util.Helper.DingInputDTO();
                        var listusers = await Db.GetIQueryable<PB_DesignList>().Where(m => m.Id == input.BillId).FirstOrDefaultAsync();
                        if (listusers != null)
                        {
                            var UserDing = await Db.GetIQueryable<Base_User>().Where(m => m.Id == listusers.CreatorId).FirstOrDefaultAsync();
                            applicantList.Add(UserDing.DingUserId);
                        }
                        var logs = await Db.GetIQueryable<Base_OverFlowLog>().OrderByDescending(m => m.CreateTime).Where(m => m.BillId == input.BillId).FirstOrDefaultAsync();
                        var user = await Db.GetIQueryable<Base_User>().Where(m => m.Id == logs.CreatorId).FirstOrDefaultAsync();

                        result.Str = "设计清单:" + ProjectModel.Name + BuildingnumberModel.Buildingnumber + user.RealName + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]" + "已完成";
                        result.ExecutorIds = applicantList;
                        result.MessgUrl = $"eapp://pages/infoView/infoView?BillId={input.BillId}&OverFlowId={input.Id}";
                        DingHelper.sendMessage(result);
                        string sql = "update PB_DesignList set ExamineStatus=2 where Id='" + input.BillId + "'";
                        await Db.ExecuteSqlAsync(sql);
                        ////钉钉完成推送抄送人
                        //List<String> Udinglist = new List<string>();
                        //Util.Helper.DingInputDTO CCresult = new Util.Helper.DingInputDTO();
                        //StringBuilder sql = new StringBuilder();

                        //string uidssreturnDesignList = "";
                        //var flowDesignList = await Db.GetListBySqlAsync<CurrentOverFlowDTO>(sql.ToString());
                        //JArray farrDesignList = JArray.Parse(flowDesignList[0].Content);
                        //JObject _objDesignList = JObject.Parse(farrDesignList[flowDesignList[0].Level].ToString());
                        //JToken[] ApproverLists = _objDesignList["Assignees"]["PerSonId"].ToArray();
                        //if (ApproverLists.Length > 0)
                        //{
                        //    foreach (var udingid in ApproverLists)
                        //    {
                        //        string userid = udingid.ToString();
                        //        var base_user = await Db.GetIQueryable<Base_User>().Where(m => m.Id == userid).FirstOrDefaultAsync();
                        //        if (base_user != null)
                        //        {
                        //            Udinglist.Add(base_user.DingUserId);
                        //        }
                        //    }
                        //}
                        //else
                        //{
                        //    string rid = _objDesignList["Assignees"]["RoleId"].ToString();
                        //    var emps = await Db.GetListBySqlAsync<Base_User>("SELECT * FROM Base_User WHERE UserPost = (SELECT RoleName FROM Base_Role WHERE Id='" + rid + "')");
                        //    foreach (var e in emps)
                        //    {
                        //        Udinglist.Add(e.DingUserId);
                        //    }
                        //}
                        //CCresult.Str = "设计清单:" + ProjectModel.Name + BuildingnumberModel.Buildingnumber + user.RealName + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]" + "已完成";
                        //CCresult.BillId = input.BillId;
                        //CCresult.ExecutorIds = Udinglist;
                        //CCresult.MessgUrl = $"eapp://pages/infoView/infoView?BillId={input.BillId}";
                        //DingHelper.sendMessage(result);
                    }


                    //进入进场通知操作
                    var bb = await Db.GetIQueryable<PB_MobilizationNotice>().Where(m => m.Id == input.BillId).FirstOrDefaultAsync();
                    if (bb != null)
                    {
                        List<string> applicantList = new List<string>();
                        var Base_OverFlowLogTop1 = await Db.GetIQueryable<Base_OverFlowLog>().Where(m => m.BillId == input.BillId).OrderByDescending(m => m.CreateTime).FirstOrDefaultAsync();
                        var baseuser = await Db.GetIQueryable<Base_User>().Where(m => m.Id == Base_OverFlowLogTop1.CreatorId).FirstOrDefaultAsync();
                        List<string> UserList = new List<string>();
                        Util.Helper.DingInputDTO result = new Util.Helper.DingInputDTO();
                        var listusers = await Db.GetIQueryable<PB_MobilizationNotice>().Where(m => m.Id == input.BillId).FirstOrDefaultAsync();
                        if (listusers != null)
                        {
                            var UserDing = await Db.GetIQueryable<Base_User>().Where(m => m.Id == listusers.CreatorId).FirstOrDefaultAsync();
                            applicantList.Add(UserDing.DingUserId);
                        }
                        var logs = await Db.GetIQueryable<Base_OverFlowLog>().OrderByDescending(m => m.CreateTime).Where(m => m.BillId == input.BillId).FirstOrDefaultAsync();
                        var user = await Db.GetIQueryable<Base_User>().Where(m => m.Id == logs.CreatorId).FirstOrDefaultAsync();
                        var item = Db.GetIQueryable<PB_MobilzationItem>().Where(m => m.ParentId == listusers.Id).ToList();
                        string Buing = "";
                        foreach (var buing in item)
                        {
                            Buing += buing.BuildingNo;
                        }
                        result.Str = "进场通知:" + listusers.ProjectName + Buing + user.RealName + "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]" + "已完成"; ;
                        result.ExecutorIds = applicantList;
                        result.MessgUrl = $"eapp://pages/infoView/infoView?BillId={input.BillId}&OverFlowId={input.Id}";
                        DingHelper.sendMessage(result);
                    }

                }

            }
            catch (Exception ex)
            {

                throw;
            }
        }
        public async Task RejectDataAsync(OverFlowInputDTO input)
        {

            //先获取当前的级别
            var task = await Db.GetListBySqlAsync<Base_OverFlowBill>("SELECT * FROM Base_OverFlowTask WHERE BillId='" + input.BillId + "'");
            int currlevel = (int)task[0].Level;
            if (currlevel == 0)
            {
                string sql = "delete from  Base_OverFlowLog where BillId='" + input.BillId + "' AND  OverFlowId='" + input.Id + "'";
                await Db.ExecuteSqlAsync(sql);
            }
            //获取工作流中设置的全部级别
            var flow = await Db.GetListBySqlAsync<Base_OverFlow>("select * from Base_OverFlow where Id='" + input.Id + "'");
            if (flow.Count == 0)
            {
                throw new BusException("");
            }
            JArray farr = JArray.Parse(flow[0].Content);
            JObject currobj = JObject.Parse(farr[currlevel].ToString());
            Base_OverFlowLog log = new Base_OverFlowLog();
            if (!currobj["Assignees"]["UserId"].ToString().Contains(_operator.UserId))
            {
                throw new BusException("您的审批已通过!");
            }
            //如果是第一级

            //删除任务表
            await Db.ExecuteSqlAsync("DELETE FROM Base_OverFlowTask WHERE BillId='" + input.BillId + "'");
            //插入log表
            log.Id = IdHelper.GetId();
            log.CreatorId = _operator.UserId;
            log.CreateTime = DateTime.Now;
            log.OverFlowId = input.Id;
            log.OverFlowDetailID = currobj["StartState"]["Id"].ToString();
            log.BillType = task[0].BillType;
            log.BillId = input.BillId;
            log.AssignTo = "";
            log.Remark = "驳回";
            log.Reason = input.Reason;
            await Db.InsertAsync<Base_OverFlowLog>(log);
            var logs = await Db.GetIQueryable<Base_OverFlowLog>().OrderByDescending(m => m.BillId == input.BillId).FirstOrDefaultAsync();
            var Users = await Db.GetIQueryable<Base_User>().Where(m => m.Id == logs.CreatorId).FirstOrDefaultAsync();
            switch (task[0].BillType)
            {
                case "DeliveryNotice"://发货通知
                                      //获取审批人
                    List<string> UserList = new List<string>();
                    //获取申请人
                    List<string> applicantList = new List<string>();

                    Util.Helper.DingInputDTO Applicantresuit = new Util.Helper.DingInputDTO();
                    var listusers = await Db.GetIQueryable<PB_DeliveryNotice>().Where(m => m.Id == input.BillId).FirstOrDefaultAsync();
                    //查询出发货通知单申请人
                    if (listusers != null)
                    {
                        var UserDing = await Db.GetIQueryable<Base_User>().Where(m => m.Id == listusers.CreatorId).FirstOrDefaultAsync();
                        applicantList.Add(UserDing.DingUserId);
                    }
                    var list = Db.GetIQueryable<PB_DeliveryNoticeItem>().Where(m => m.ParentId == listusers.Id).ToList();
                    string buind = "";
                    foreach (var li in list)
                    {
                        buind += li.BuildingNo;
                    }

                    Applicantresuit.BillId = input.BillId;
                    Applicantresuit.ExecutorIds = applicantList;
                    Applicantresuit.Str = "发货通知:" + listusers.ProjectName + buind + "审批人:" + Users.RealName + "已驳回 驳回原因:" + input.Reason;
                    Applicantresuit.MessgUrl = $"eapp://pages/infoView/infoView?BillId={input.BillId}&OverFlowId={input.Id}";
                    DingHelper.sendMessage(Applicantresuit);
                    break;
                case "MobilizationNotice"://进场通知
                    List<string> applicantLists = new List<string>();
                    List<string> ulist = new List<string>();
                    Util.Helper.DingInputDTO Applicantresuits = new Util.Helper.DingInputDTO();
                    var listuserss = await Db.GetIQueryable<PB_MobilizationNotice>().Where(m => m.Id == input.BillId).FirstOrDefaultAsync();
                    if (listuserss != null)
                    {
                        var UserDing = await Db.GetIQueryable<Base_User>().Where(m => m.Id == listuserss.CreatorId).FirstOrDefaultAsync();
                        applicantLists.Add(UserDing.DingUserId);
                    }
                    var listMobilizat = Db.GetIQueryable<PB_MobilzationItem>().Where(m => m.ParentId == listuserss.Id).ToList();
                    string buindMobilizat = "";
                    foreach (var li in listMobilizat)
                    {
                        buindMobilizat += li.BuildingNo;
                    }
                    Applicantresuits.BillId = input.BillId;
                    Applicantresuits.ExecutorIds = applicantLists;
                    Applicantresuits.Str = "进场通知:" + listuserss.ProjectName + buindMobilizat + Users.RealName + "已驳回 驳回原因:" + input.Reason;
                    Applicantresuits.MessgUrl = $"eapp://pages/infoView/infoView?BillId={input.BillId}&OverFlowId={input.Id}";
                    DingHelper.sendMessage(Applicantresuits);
                    break;
                case "ReturnNotice"://发货通知
                                    //获取审批人
                    List<string> UserListreturn = new List<string>();
                    //获取申请人
                    List<string> applicantListreturn = new List<string>();

                    Util.Helper.DingInputDTO Applicantresuitreturn = new Util.Helper.DingInputDTO();
                    var listusersreturn = await Db.GetIQueryable<PB_DeliveryNotice>().Where(m => m.Id == input.BillId).FirstOrDefaultAsync();
                    //查询出发货通知单申请人
                    if (listusersreturn != null)
                    {
                        var UserDing = await Db.GetIQueryable<Base_User>().Where(m => m.Id == listusersreturn.CreatorId).FirstOrDefaultAsync();
                        applicantListreturn.Add(UserDing.DingUserId);
                    }


                    Applicantresuitreturn.BillId = input.BillId;
                    Applicantresuitreturn.ExecutorIds = applicantListreturn;
                    Applicantresuitreturn.Str = "回货通知:" + listusersreturn.ProjectName + "审批人:" + Users.RealName + "已驳回 驳回原因:" + input.Reason;
                    Applicantresuitreturn.MessgUrl = $"eapp://pages/infoView/infoView?BillId={input.BillId}&OverFlowId={input.Id}";
                    DingHelper.sendMessage(Applicantresuitreturn);
                    break;
                case "DesignList"://设计清单
                    var PB_DesignListModel = await Db.GetIQueryable<PB_DesignList>().Where(m => m.Id == input.BillId).FirstOrDefaultAsync();
                    var ProjectModel = await Db.GetIQueryable<PB_Project>().Where(m => m.Id == PB_DesignListModel.ProjectId).FirstOrDefaultAsync();
                    var BuildingnumberModel = await Db.GetIQueryable<PB_Buildingnumber>().Where(m => m.Id == PB_DesignListModel.BuildingNumberID).FirstOrDefaultAsync();

                    List<string> applicantListreturnss = new List<string>();

                    Util.Helper.DingInputDTO Applicantresuitreturnss = new Util.Helper.DingInputDTO();
                    var listusersreturnss = await Db.GetIQueryable<PB_DesignList>().Where(m => m.Id == input.BillId).FirstOrDefaultAsync();
                    //查询出发货通知单申请人
                    if (listusersreturnss != null)
                    {
                        var UserDing = await Db.GetIQueryable<Base_User>().Where(m => m.Id == listusersreturnss.CreatorId).FirstOrDefaultAsync();
                        applicantListreturnss.Add(UserDing.DingUserId);
                    }
                    Applicantresuitreturnss.BillId = input.BillId;
                    Applicantresuitreturnss.ExecutorIds = applicantListreturnss;
                    Applicantresuitreturnss.Str = "设计清单:" + ProjectModel.Name + BuildingnumberModel.Buildingnumber + "审批人:" + Users.RealName + "已驳回 驳回原因:" + input.Reason;
                    Applicantresuitreturnss.MessgUrl = $"eapp://pages/infoView/infoView?BillId={input.BillId}&OverFlowId={input.Id}";
                    List<string> ccuserList = new List<string>();
                    DingHelper.sendMessage(Applicantresuitreturnss);
                    break;
                case "InsertDesignList":
                    var PB_DesignListModelInsert = await Db.GetIQueryable<PB_DesignList>().Where(m => m.Id == input.BillId).FirstOrDefaultAsync();
                    var ProjectModelInsert = await Db.GetIQueryable<PB_Project>().Where(m => m.Id == PB_DesignListModelInsert.ProjectId).FirstOrDefaultAsync();
                    var BuildingnumberModelInsert = await Db.GetIQueryable<PB_Buildingnumber>().Where(m => m.Id == PB_DesignListModelInsert.BuildingNumberID).FirstOrDefaultAsync();

                    List<string> applicantListreturnssInsert = new List<string>();

                    Util.Helper.DingInputDTO ApplicantresuitreturnssInsert = new Util.Helper.DingInputDTO();
                    var listusersreturnssInsert = await Db.GetIQueryable<PB_DesignList>().Where(m => m.Id == input.BillId).FirstOrDefaultAsync();
                    //查询出发货通知单申请人
                    if (listusersreturnssInsert != null)
                    {
                        var UserDing = await Db.GetIQueryable<Base_User>().Where(m => m.Id == listusersreturnssInsert.CreatorId).FirstOrDefaultAsync();
                        applicantListreturnssInsert.Add(UserDing.DingUserId);
                    }
                    ApplicantresuitreturnssInsert.BillId = input.BillId;
                    ApplicantresuitreturnssInsert.ExecutorIds = applicantListreturnssInsert;
                    ApplicantresuitreturnssInsert.Str = "设计清单补加单:" + ProjectModelInsert.Name + BuildingnumberModelInsert.Buildingnumber + "审批人:" + Users.RealName + "已驳回 驳回原因:" + input.Reason;
                    ApplicantresuitreturnssInsert.MessgUrl = $"eapp://pages/infoView/infoView?BillId={input.BillId}&OverFlowId={input.Id}";
                    List<string> ccuserListInsert = new List<string>();
                    DingHelper.sendMessage(ApplicantresuitreturnssInsert);
                    break;
                case "":
                    var PB_DesignListModelChange = await Db.GetIQueryable<PB_DesignList>().Where(m => m.Id == input.BillId).FirstOrDefaultAsync();
                    var ProjectModelChange = await Db.GetIQueryable<PB_Project>().Where(m => m.Id == PB_DesignListModelChange.ProjectId).FirstOrDefaultAsync();
                    var BuildingnumberModelChange = await Db.GetIQueryable<PB_Buildingnumber>().Where(m => m.Id == PB_DesignListModelChange.BuildingNumberID).FirstOrDefaultAsync();

                    List<string> applicantListreturnssChange = new List<string>();

                    Util.Helper.DingInputDTO ApplicantresuitreturnssChange = new Util.Helper.DingInputDTO();
                    var listusersreturnssChange = await Db.GetIQueryable<PB_DesignList>().Where(m => m.Id == input.BillId).FirstOrDefaultAsync();
                    //查询出发货通知单申请人
                    if (listusersreturnssChange != null)
                    {
                        var UserDing = await Db.GetIQueryable<Base_User>().Where(m => m.Id == listusersreturnssChange.CreatorId).FirstOrDefaultAsync();
                        applicantListreturnssChange.Add(UserDing.DingUserId);
                    }
                    ApplicantresuitreturnssChange.BillId = input.BillId;
                    ApplicantresuitreturnssChange.ExecutorIds = applicantListreturnssChange;
                    ApplicantresuitreturnssChange.Str = "设计清单变更单:" + ProjectModelChange.Name + BuildingnumberModelChange.Buildingnumber + "审批人:" + Users.RealName + "已驳回 驳回原因:" + input.Reason;
                    ApplicantresuitreturnssChange.MessgUrl = $"eapp://pages/infoView/infoView?BillId={input.BillId}&OverFlowId={input.Id}";
                    List<string> ccuserListChange = new List<string>();
                    DingHelper.sendMessage(ApplicantresuitreturnssChange);
                    break;
                default:
                    break;
            }



        }
        #endregion
        #region 私有成员
        public async Task<Util.Helper.DingInputDTO> GetDingInputAsync(string billId, string billType)
        {
            Util.Helper.DingInputDTO result = new Util.Helper.DingInputDTO();
            StringBuilder sql = new StringBuilder();
            switch (billType)
            {
                case "DeliveryPlan"://发货计划
                    #region 拼接sql获取发货计划工作流明细
                    sql.Append("SELECT ");
                    sql.Append("b.Content,");
                    sql.Append("c.LEVEL ");
                    sql.Append("FROM ");
                    sql.Append("PB_DeliveryPlan a ");
                    sql.Append("LEFT JOIN Base_OverFlow b ON a.WorkFlow = b.Id ");
                    sql.Append("LEFT JOIN Base_OverFlowTask c ON a.Id = c.BillId ");
                    sql.Append("WHERE ");
                    sql.Append("a.Id = '" + billId + "'");
                    var flow = await Db.GetListBySqlAsync<CurrentOverFlowDTO>(sql.ToString());
                    JArray farr = JArray.Parse(flow[0].Content);
                    JObject _obj = JObject.Parse(farr[flow[0].Level].ToString());
                    JArray usr = JArray.Parse(_obj["Assignees"]["UserId"].ToString());
                    result.Str = _obj["ToState"]["Name"].ToString();
                    result.BillId = billId;
                    string uids = "";
                    List<string> ulist = new List<string>();
                    if (usr.Count > 0)
                    {
                        //员工
                        foreach (var u in usr)
                        {
                            uids += "'" + u + "',";
                        }
                        var emps = await Db.GetListBySqlAsync<Base_User>("SELECT * FROM Base_User WHERE Id IN (" + uids.Substring(0, uids.Length - 1) + ")");
                        foreach (var e in emps)
                        {
                            ulist.Add(e.DingUserId);
                        }
                    }
                    else
                    {
                        //角色
                        string rid = _obj["Assignees"]["RoleId"].ToString();
                        var emps = await Db.GetListBySqlAsync<Base_User>("SELECT * FROM Base_User WHERE UserPost = (SELECT RoleName FROM Base_Role WHERE Id='" + rid + "')");
                        foreach (var e in emps)
                        {
                            ulist.Add(e.DingUserId);
                        }
                    }
                    result.ExecutorIds = ulist;
                    #endregion
                    break;
                case "MobilizationNotice"://进场通知
                    #region 拼接sql获取进厂通知单工作流明细
                    sql.Append("SELECT ");
                    sql.Append("b.Content,");
                    sql.Append("ifnull(c.LEVEL,0) LEVEL ");
                    sql.Append("FROM ");
                    sql.Append("PB_MobilizationNotice a ");
                    sql.Append("LEFT JOIN Base_OverFlow b ON a.WorkFlow = b.Id ");
                    sql.Append("LEFT JOIN Base_OverFlowTask c ON a.Id = c.BillId ");
                    sql.Append("WHERE ");
                    sql.Append("a.Id = '" + billId + "'");
                    var flowMob = await Db.GetListBySqlAsync<CurrentOverFlowDTO>(sql.ToString());
                    JArray farrMob = JArray.Parse(flowMob[0].Content);
                    JObject _objMob = JObject.Parse(farrMob[flowMob[0].Level].ToString());
                    JArray usrMob = JArray.Parse(_objMob["Assignees"]["UserId"].ToString());
                    result.Str = _objMob["ToState"]["Name"].ToString();
                    result.BillId = billId;
                    string mobUids = "";
                    List<string> ulistMob = new List<string>();
                    if (usrMob.Count > 0)
                    {
                        //员工
                        foreach (var u in usrMob)
                        {
                            mobUids += "'" + u + "',";
                        }
                        var emps = await Db.GetListBySqlAsync<Base_User>("SELECT * FROM Base_User WHERE Id IN (" + mobUids.Substring(0, mobUids.Length - 1) + ")");
                        foreach (var e in emps)
                        {
                            ulistMob.Add(e.DingUserId);
                        }
                    }
                    else
                    {
                        //角色
                        string rid = _objMob["Assignees"]["RoleId"].ToString();
                        var emps = await Db.GetListBySqlAsync<Base_User>("SELECT * FROM Base_User WHERE UserPost = (SELECT RoleName FROM Base_Role WHERE Id='" + rid + "')");
                        foreach (var e in emps)
                        {
                            ulistMob.Add(e.DingUserId);
                        }
                    }
                    result.ExecutorIds = ulistMob;
                    #endregion
                    break;
                case "DeliveryNotice":
                    #region 拼接sql发货通知数据获取工作流明细
                    sql.Append("Select ");
                    sql.Append("b.Content, ");
                    sql.Append("ifnull(c.LEVEL,0) LEVEL ");
                    sql.Append("FROM ");
                    sql.Append("PB_DeliveryNotice a ");
                    sql.Append("LEFT JOIN Base_OverFlow b ON a.WorkFlow = b.Id ");
                    sql.Append("LEFT JOIN Base_OverFlowTask c ON a.Id = c.BillId ");
                    sql.Append("WHERE ");
                    sql.Append("a.Id = '" + billId + "'");
                    var DeliveryNoticeFlowMob = await Db.GetListBySqlAsync<CurrentOverFlowDTO>(sql.ToString());
                    JArray NoticeFlowMob = JArray.Parse(DeliveryNoticeFlowMob[0].Content);
                    JObject NoticeobjMob = JObject.Parse(NoticeFlowMob[DeliveryNoticeFlowMob[0].Level].ToString());
                    JArray NoticeUsrMob = JArray.Parse(NoticeobjMob["Assignees"]["UserId"].ToString());
                    result.Str = NoticeUsrMob["ToState"]["Name"].ToString();
                    result.BillId = billId;
                    string NoticemobUids = "";
                    List<string> NoticeulistMob = new List<string>();
                    if (NoticeUsrMob.Count > 0)
                    {
                        foreach (var u in NoticeUsrMob)
                        {
                            NoticemobUids += "'" + u + "',";
                        }
                        var emps = await Db.GetListBySqlAsync<Base_User>("SELECT * FROM Base_User WHERE Id IN (" + NoticemobUids.Substring(0, NoticemobUids.Length - 1) + ")");
                        foreach (var e in emps)
                        {
                            NoticeulistMob.Add(e.DingUserId);
                        }
                    }
                    else
                    {
                        string Noticeurid = NoticeobjMob["Assignees"]["RoleId"].ToString();
                        var emps = await Db.GetListBySqlAsync<Base_User>("SELECT * FROM Base_User WHERE UserPost = (SELECT RoleName FROM Base_Role WHERE Id='" + Noticeurid + "')");
                        foreach (var item in emps)
                        {
                            NoticeulistMob.Add(item.DingUserId);
                        }
                    }
                    result.ExecutorIds = NoticeulistMob;
                    #endregion
                    break;
                default:
                    break;
            }
            return result;
        }

        public async Task<List<Base_OverFlowTask>> GetTaskDataListInfoAsync(string ProjectId, string BuildingnumberId)
        {
            List<Base_OverFlowTask> result = new List<Base_OverFlowTask>();
            List<Base_OverFlowCfg> base_OverFlowCfgs = new List<Base_OverFlowCfg>();
            //判断传进来的项目id和楼号id 比对发货通知
            var basecfg = await Db.GetListBySqlAsync<Base_OverFlowCfg>("select a.*,b.Id,b.Title,b.Content from Base_OverFlowTask a left join Base_OverFlow b on a.BillType=b.BillType    LEFT JOIN PB_DeliveryNotice  as d on a.BillId = d.Id LEFT JOIN  PB_DeliveryNoticeItem as e ON e.ParentId = d.Id LEFT JOIN  PB_DesignList as l ON e.DesignId = l.Id WHERE d.ProjectId = '" + ProjectId + "' AND l.BuildingNumberID = '" + BuildingnumberId + "' ");
            if (basecfg.Count > 0)
            {
                foreach (var i in basecfg)
                {
                    base_OverFlowCfgs.Add(i);
                }
            }
            //判断传进来的项目id和楼号id 比对进场通知
            var PB_MobilizationNoticelIST = await Db.GetListBySqlAsync<Base_OverFlowCfg>("select a.*,b.Id,b.Title,b.Content FROM 	Base_OverFlowTask a  LEFT JOIN Base_OverFlow b ON a.BillType = b.BillType  LEFT JOIN PB_MobilizationNotice AS d ON a.BillId = d.Id  LEFT JOIN PB_MobilzationItem AS e ON e.ParentId = d.Id	WHERE d.ProjectId = '" + ProjectId + "' AND e.BuildingNumberID = '" + BuildingnumberId + "'");
            if (PB_MobilizationNoticelIST.Count > 0)
            {
                foreach (var i in PB_MobilizationNoticelIST)
                {
                    base_OverFlowCfgs.Add(i);
                }
            }
            //判断传进来的项目id和楼号id比对发货计划
            var DeliveryPlanData = await Db.GetListBySqlAsync<Base_OverFlowCfg>("select a.*,b.Id,b.Title,b.Content FROM 	Base_OverFlowTask a  LEFT JOIN Base_OverFlow b ON a.BillType = b.BillType  LEFT JOIN PB_DeliveryPlan AS d ON a.BillId = d.Id  LEFT JOIN PB_DeliveryPlanDetails AS e ON e.ParentId = d.Id WHERE e.ProjectId = '" + ProjectId + "'  AND e.BuildingId = '" + BuildingnumberId + "'");
            if (DeliveryPlanData.Count > 0)
            {
                foreach (var i in DeliveryPlanData)
                {
                    base_OverFlowCfgs.Add(i);
                }
            }
            if (base_OverFlowCfgs.Count > 0)
            {
                foreach (var item in base_OverFlowCfgs)
                {
                    switch (item.BillType)
                    {
                        case "DeliveryNotice":
                            StringBuilder sql = new StringBuilder();
                            sql.Append("SELECT ");
                            sql.Append("b.ProjectName, ");
                            sql.Append("a.BuildingNo Buildingnumber ");
                            sql.Append("FROM ");
                            sql.Append("PB_DeliveryNoticeItem AS a  ");
                            sql.Append("LEFT JOIN PB_DeliveryNotice AS b ON a.ParentId = b.Id   ");
                            sql.Append("LEFT JOIN PB_DesignList AS c ON a.DesignId = c.Id  ");
                            sql.Append("LEFT JOIN Base_OverFlowTask AS t ON b.Id = t.BillId   ");
                            sql.Append("WHERE ");
                            sql.Append("b.ProjectId = '" + ProjectId + "' AND c.BuildingNumberID = '" + BuildingnumberId + "'");
                            var _msgs = await Db.GetListBySqlAsync<DD_MessageDTO>(sql.ToString());
                            foreach (var _msg in _msgs)
                            {
                                JArray jarr = JArray.Parse(item.Content);
                                JObject jobj = JObject.Parse(jarr[item.Level].ToString());
                                string txt = jobj["StartState"]["Name"].ToString();
                                string rid = jobj["Assignees"]["RoleId"].ToString();
                                string btn = jobj["Action"]["Name"].ToString();
                                JArray usr = JArray.Parse(jobj["Assignees"]["UserId"].ToString());
                                if (usr.Count() > 0)
                                {
                                    foreach (var items in usr)
                                    {
                                        string _u = items.ToString();
                                        if (_u.Equals(_operator.UserId))
                                        {
                                            Base_OverFlowTask temp = new Base_OverFlowTask();
                                            temp.Id = item.Id;
                                            temp.BillId = item.BillId;
                                            temp.Title = DateTime.Now.ToString("g") + txt;
                                            temp.BtnName = btn;
                                            temp.BillType = item.BillType;
                                            temp.Rule = 0;
                                            result.Add(temp);
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    //角色
                                    if (_operator.Property.RoleIdList.Contains(rid))
                                    {
                                        Base_OverFlowTask temp = new Base_OverFlowTask();
                                        temp.Id = item.Id;
                                        temp.BillId = item.BillId;
                                        temp.BillType = item.BillType;
                                        temp.Title = item.Title;
                                        temp.Rule = 0;
                                        result.Add(temp);
                                    }
                                }



                            }
                            break;
                        case "MobilizationNotice":
                            StringBuilder SQLMobilizationNotice = new StringBuilder();
                            SQLMobilizationNotice.Append("SELECT ");
                            SQLMobilizationNotice.Append("b.ProjectName, ");
                            SQLMobilizationNotice.Append("e.Buildingnumber BuildingNo   ");
                            SQLMobilizationNotice.Append("FROM  ");
                            SQLMobilizationNotice.Append("PB_MobilzationItem AS a ");
                            SQLMobilizationNotice.Append("LEFT JOIN PB_MobilizationNotice AS b ON a.ParentId = b.Id  ");
                            SQLMobilizationNotice.Append("LEFT JOIN Base_OverFlowTask as c on b.Id=c.BillId  ");
                            SQLMobilizationNotice.Append("LEFT JOIN PB_Buildingnumber AS e ON a.BuildingnumberId = e.Id ");
                            SQLMobilizationNotice.Append("WHERE ");
                            SQLMobilizationNotice.Append("  b.ProjectId = '" + ProjectId + "' AND a.BuildingnumberId='" + BuildingnumberId + "'");
                            var SQLMobilizationNoticeList = await Db.GetListBySqlAsync<DD_MessageDTO>(SQLMobilizationNotice.ToString());
                            foreach (var _msg in SQLMobilizationNoticeList)
                            {
                                JArray jarr = JArray.Parse(item.Content);
                                JObject jobj = JObject.Parse(jarr[item.Level].ToString());
                                string txt = jobj["StartState"]["Name"].ToString();
                                string rid = jobj["Assignees"]["RoleId"].ToString();
                                string btn = jobj["Action"]["Name"].ToString();
                                JArray usr = JArray.Parse(jobj["Assignees"]["UserId"].ToString());
                                if (usr.Count() > 0)
                                {
                                    foreach (var items in usr)
                                    {
                                        string _u = items.ToString();
                                        if (_u.Equals(_operator.UserId))
                                        {
                                            Base_OverFlowTask temp = new Base_OverFlowTask();
                                            temp.Id = item.Id;
                                            temp.BillId = item.BillId;
                                            temp.Title = DateTime.Now.ToString("g") + txt;
                                            temp.BtnName = btn;
                                            temp.BillType = item.BillType;
                                            temp.Rule = 0;
                                            result.Add(temp);
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    //角色
                                    if (_operator.Property.RoleIdList.Contains(rid))
                                    {
                                        Base_OverFlowTask temp = new Base_OverFlowTask();
                                        temp.Id = item.Id;
                                        temp.BillId = item.BillId;
                                        temp.BillType = item.BillType;
                                        temp.Title = item.Title;
                                        temp.Rule = 0;
                                        result.Add(temp);
                                    }
                                }



                            }
                            break;
                        case "":
                            break;

                    }
                }
            }
            return result;
        }

        public async Task<string> GetTheBillType(string Id)
        {
            string BillType = "";
            var _BillTypevalue = await Db.GetIQueryable<Base_OverFlow>().Where(m => m.Id == Id).FirstOrDefaultAsync();
            BillType = _BillTypevalue.BillType;
            return BillType;
        }

        public async Task<int?> GetTheDesignState(string Id)
        {
            int? BillType = 0;
            var _BillTypevalue = await Db.GetIQueryable<PB_DesignList>().Where(m => m.Id == Id).FirstOrDefaultAsync();
            BillType = _BillTypevalue.ExamineStatus;
            return BillType;
        }


        #endregion
    }
}